Advertisement
pasholnahuy

Untitled

Nov 4th, 2023
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. def get_df(sellers, items, products, orders):
  2. merged_df = sellers.merge(items, on = 'seller_id')
  3. df = products.merge(merged_df, on = 'product_id')
  4. df = df[['seller_state', 'product_category_name', 'order_id']].merge(orders[['order_status', 'order_id', 'order_delivered_customer_date']], on = 'order_id')
  5. df.rename(columns = {'product_category_name': 'category', 'seller_state': 'state', 'order_status': 'status', 'order_delivered_customer_date' : 'year'}, inplace = True)
  6. df['date'] = df['date'].apply(lambda x: pd.to_datetime(x).year)
  7. return df
  8.  
  9.  
  10. app = Dash(__name__)
  11.  
  12. app.layout = html.Div([
  13. dcc.Graph(id='sellers-graph'),
  14. dcc.Slider(
  15. df['year'].min(),
  16. df['year'].max(),
  17. step=None,
  18. value=df['year'].min(),
  19. marks={str(year): str(year) for year in df['year'].unique()},
  20. id='year-slider'
  21. )
  22. dcc.Checklist(
  23. df['state'],
  24. df['state'],
  25. id = 'state-slider'
  26. )
  27. ])
  28.  
  29. df = get_df(sellers, items, products, orders)
  30.  
  31. @app.callback(
  32. Output('sellers-graph', 'figure'),
  33. [Input('date-slider', 'value')
  34. Input('states-dropdown', 'value'),
  35. Input('statuses-filter', 'value')])
  36.  
  37. def update_figure(time_gap, state_to_display, statuses_filter):
  38. df = df.groupby(['state', 'category', 'status']).agg({'order_id' : 'count'}).reset_index()
  39. df.rename(columns = {'order_id' : 'count'}, inplace = True)
  40. df = df.loc[df.state == state_to_display & df.statys = statuses_filter & df.time_gap]
  41. fig = px.bar(table_1, y="customer_state", x="count", color="category",
  42. title="Distribution by sales categories", orientation='h', height=300)
  43.  
  44. fig.update_layout(transition_duration=500)
  45. return fig
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement