Advertisement
pasholnahuy

Untitled

Nov 5th, 2023
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.47 KB | None | 0 0
  1. #MY DATA
  2. #product, category
  3. myproduct = products[['product_id', 'product_category_name']].drop_duplicates()
  4. myproduct = myproduct.rename(columns={'product_category_name' : 'category'})
  5. #order, customer, seller, date
  6. myorders = orders[['order_id', 'customer_id', 'order_purchase_timestamp', 'order_status']].drop_duplicates()
  7. myorders = myorders.rename(columns={'order_purchase_timestamp': 'date'})
  8. myorders['date'] = pd.to_datetime(myorders['date']).dt.date
  9. time_period = myorders[['date']]
  10. time_period = time_period.sort_values(by='date')
  11. marks = {i: str(sorted(all_dates['date'])[i]) for i in range(0, all_dates.shape[0], 7850)}
  12. #seller, order, product
  13. myitems = items[['seller_id', 'order_id', 'product_id']].drop_duplicates()
  14. #seller, seller_state
  15. mysellers = sellers[['seller_id', 'seller_state']].drop_duplicates()
  16. #customer, customer_stat
  17. mycustomers = customers[['customer_id', 'customer_state']].drop_duplicates()
  18. unique_states = ['All'] + sorted(list(set(list(sellers.seller_state.unique()) + list(customers.customer_state.unique()))))
  19. #statuses
  20. unique_statuses = orders.order_status.unique()
  21.  
  22.  
  23. app = Dash(__name__)
  24.  
  25. app.layout = html.Div([
  26. html.Label('Order statuses to display:', style={'font-size': '120%'}),
  27. dcc.Checklist(options=unique_statuses, value=unique_statuses, inline=True, id='statuses-filter'),
  28. html.Label('\nSelect state:', style={'font-size': '120%'}),
  29. html.Div(dcc.Dropdown(unique_states, 'All', id='states-dropdown'),
  30. style={'width': '200px', 'display': 'inline-block'}),
  31. dcc.Graph(id='sellers-graph'),
  32. dcc.Graph(id='seller-scatter'),
  33. dcc.Graph(id='customers-graph'),
  34. dcc.RangeSlider(
  35. min=0,
  36. max=time_period.shape[0] - 1,
  37. step=1,
  38. dots=False,
  39. value=[0, time_period.shape[0] - 1],
  40. marks=marks, allowCross=False,
  41. updatemode='mouseup',
  42. id='date-slider',
  43. ),
  44.  
  45. ])
  46.  
  47.  
  48. @app.callback(
  49. Output('customers-graph', 'figure'),
  50. [Input('date-slider', 'value'),
  51. Input('states-dropdown', 'value'),
  52. Input('statuses-filter', 'value')])
  53. def update_figure(time_gap, state_to_display, statuses_filter, *args, **kwargs):
  54. start = time_gap[0]
  55. finish = time_gap[1]
  56.  
  57. customer_distribution = myproduct.merge(myitems, on='product_id')
  58. customer_distribution = customer_distribution.drop_duplicates()
  59. customer_distribution = customer_distribution.groupby(['customer_id', 'category', 'order_id']).agg({'product_id' : 'count'}).reset_index()
  60. customer_distribution = customer_distribution.rename(columns={'product_id' : 'quantity'})
  61. customer_distribution = customer_distribution.merge(mysellers, on='seller_id')
  62. customer_distribution = customer_distribution.merge(myorders[['order_id', 'order_status', 'date']].drop_duplicates(), on='order_id')
  63. customer_distribution = customer_distribution[sorted(all_dates['date'])[start] <= customer_distribution.date]
  64. customer_distribution = customer_distribution[customer_distribution.date <= sorted(all_dates['date'])[finish]]
  65. customer_distribution = customer_distribution[customer_distribution['order_status'].isin(statuses_filter)]
  66. if state_to_display != 'All':
  67. customer_distribution = customer_distribution.loc[customer_distribution['seller_state'] == state_to_display]
  68. else:
  69. customer_distribution['customer_state'] = 'All states'
  70. customer_distribution = customer_distribution.drop(columns=['date'])
  71. customer_distribution = customer_distribution.groupby(['category', 'customer_state']).agg({'quantity' : 'sum'}).reset_index()
  72. overall = sum(customer_distribution['quantity'])
  73. customer_distribution['percentage'] = 100 * customer_distribution['quantity'] / overall
  74. df = customer_distribution[['category', 'percentage', 'customer_state']]
  75. #fig = px.scatter(df, y="percentage", x="category")
  76. fig = px.scatter(
  77. df, y="percentage", x="category", color="percentage",
  78. labels={"customer_state": "Customer State"},
  79. title="Seller Distribution by Category",
  80. height=300
  81. )
  82. fig.update_layout(transition_duration=500)
  83. return fig
  84.  
  85. @app.callback(
  86. Output('seller-scatter', 'figure'),
  87. [Input('date-slider', 'value'),
  88. Input('states-dropdown', 'value'),
  89. Input('statuses-filter', 'value')])
  90. def update_figure(time_gap, state_to_display, statuses_filter, *args, **kwargs):
  91. start = time_gap[0]
  92. finish = time_gap[1]
  93.  
  94. seller_distribution = myproduct.merge(myitems, on='product_id')
  95. seller_distribution = seller_distribution.drop_duplicates()
  96. seller_distribution = seller_distribution.groupby(['seller_id', 'category', 'order_id']).agg({'product_id' : 'count'}).reset_index()
  97. seller_distribution = seller_distribution.rename(columns={'product_id' : 'quantity'})
  98. seller_distribution = seller_distribution.merge(mysellers, on='seller_id')
  99. seller_distribution = seller_distribution.merge(myorders[['order_id', 'order_status', 'date']].drop_duplicates(), on='order_id')
  100. seller_distribution = seller_distribution[sorted(all_dates['date'])[start] <= seller_distribution.date]
  101. seller_distribution = seller_distribution[seller_distribution.date <= sorted(all_dates['date'])[finish]]
  102. seller_distribution = seller_distribution[seller_distribution['order_status'].isin(statuses_filter)]
  103. if state_to_display != 'All':
  104. seller_distribution = seller_distribution.loc[seller_distribution['seller_state'] == state_to_display]
  105. else:
  106. seller_distribution['seller_state'] = 'All states'
  107. seller_distribution = seller_distribution.drop(columns=['date'])
  108. seller_distribution = seller_distribution.groupby(['category', 'seller_state']).agg({'quantity' : 'sum'}).reset_index()
  109. overall = sum(seller_distribution['quantity'])
  110. seller_distribution['percentage'] = 100 * seller_distribution['quantity'] / overall
  111. df = seller_distribution[['category', 'percentage', 'seller_state']]
  112. fig = px.bar(
  113. df, y="seller_state", x="percentage", color="category", color_continuous_scale="emrld",
  114. labels={"seller_state": "Seller State"},
  115. title="Distribution by Sales Categories",
  116. orientation='h',
  117. height=300
  118. )
  119. #fig = px.bar(df, y="seller_state", x="percentage", color="category", title="Distribution by sales categories",
  120. #orientation='h', height=300)
  121.  
  122. fig.update_layout(transition_duration=500)
  123. return fig
  124.  
  125.  
  126.  
  127. if __name__ == '__main__':
  128. app.run_server(debug=True, port=8095)
  129.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement