Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #MY DATA
- #product, category
- myproduct = products[['product_id', 'product_category_name']].drop_duplicates()
- myproduct = myproduct.rename(columns={'product_category_name' : 'category'})
- #order, customer, seller, date
- myorders = orders[['order_id', 'customer_id', 'order_purchase_timestamp', 'order_status']].drop_duplicates()
- myorders = myorders.rename(columns={'order_purchase_timestamp': 'date'})
- myorders['date'] = pd.to_datetime(myorders['date']).dt.date
- time_period = myorders[['date']]
- time_period = time_period.sort_values(by='date')
- marks = {i: str(sorted(all_dates['date'])[i]) for i in range(0, all_dates.shape[0], 7850)}
- #seller, order, product
- myitems = items[['seller_id', 'order_id', 'product_id']].drop_duplicates()
- #seller, seller_state
- mysellers = sellers[['seller_id', 'seller_state']].drop_duplicates()
- #customer, customer_stat
- mycustomers = customers[['customer_id', 'customer_state']].drop_duplicates()
- unique_states = ['All'] + sorted(list(set(list(sellers.seller_state.unique()) + list(customers.customer_state.unique()))))
- #statuses
- unique_statuses = orders.order_status.unique()
- app = Dash(__name__)
- app.layout = html.Div([
- html.Label('Order statuses to display:', style={'font-size': '120%'}),
- dcc.Checklist(options=unique_statuses, value=unique_statuses, inline=True, id='statuses-filter'),
- html.Label('\nSelect state:', style={'font-size': '120%'}),
- html.Div(dcc.Dropdown(unique_states, 'All', id='states-dropdown'),
- style={'width': '200px', 'display': 'inline-block'}),
- dcc.Graph(id='sellers-graph'),
- dcc.Graph(id='seller-scatter'),
- dcc.Graph(id='customers-graph'),
- dcc.RangeSlider(
- min=0,
- max=time_period.shape[0] - 1,
- step=1,
- dots=False,
- value=[0, time_period.shape[0] - 1],
- marks=marks, allowCross=False,
- updatemode='mouseup',
- id='date-slider',
- ),
- ])
- @app.callback(
- Output('customers-graph', 'figure'),
- [Input('date-slider', 'value'),
- Input('states-dropdown', 'value'),
- Input('statuses-filter', 'value')])
- def update_figure(time_gap, state_to_display, statuses_filter, *args, **kwargs):
- start = time_gap[0]
- finish = time_gap[1]
- customer_distribution = myproduct.merge(myitems, on='product_id')
- customer_distribution = customer_distribution.drop_duplicates()
- customer_distribution = customer_distribution.groupby(['customer_id', 'category', 'order_id']).agg({'product_id' : 'count'}).reset_index()
- customer_distribution = customer_distribution.rename(columns={'product_id' : 'quantity'})
- customer_distribution = customer_distribution.merge(mysellers, on='seller_id')
- customer_distribution = customer_distribution.merge(myorders[['order_id', 'order_status', 'date']].drop_duplicates(), on='order_id')
- customer_distribution = customer_distribution[sorted(all_dates['date'])[start] <= customer_distribution.date]
- customer_distribution = customer_distribution[customer_distribution.date <= sorted(all_dates['date'])[finish]]
- customer_distribution = customer_distribution[customer_distribution['order_status'].isin(statuses_filter)]
- if state_to_display != 'All':
- customer_distribution = customer_distribution.loc[customer_distribution['seller_state'] == state_to_display]
- else:
- customer_distribution['customer_state'] = 'All states'
- customer_distribution = customer_distribution.drop(columns=['date'])
- customer_distribution = customer_distribution.groupby(['category', 'customer_state']).agg({'quantity' : 'sum'}).reset_index()
- overall = sum(customer_distribution['quantity'])
- customer_distribution['percentage'] = 100 * customer_distribution['quantity'] / overall
- df = customer_distribution[['category', 'percentage', 'customer_state']]
- #fig = px.scatter(df, y="percentage", x="category")
- fig = px.scatter(
- df, y="percentage", x="category", color="percentage",
- labels={"customer_state": "Customer State"},
- title="Seller Distribution by Category",
- height=300
- )
- fig.update_layout(transition_duration=500)
- return fig
- @app.callback(
- Output('seller-scatter', 'figure'),
- [Input('date-slider', 'value'),
- Input('states-dropdown', 'value'),
- Input('statuses-filter', 'value')])
- def update_figure(time_gap, state_to_display, statuses_filter, *args, **kwargs):
- start = time_gap[0]
- finish = time_gap[1]
- seller_distribution = myproduct.merge(myitems, on='product_id')
- seller_distribution = seller_distribution.drop_duplicates()
- seller_distribution = seller_distribution.groupby(['seller_id', 'category', 'order_id']).agg({'product_id' : 'count'}).reset_index()
- seller_distribution = seller_distribution.rename(columns={'product_id' : 'quantity'})
- seller_distribution = seller_distribution.merge(mysellers, on='seller_id')
- seller_distribution = seller_distribution.merge(myorders[['order_id', 'order_status', 'date']].drop_duplicates(), on='order_id')
- seller_distribution = seller_distribution[sorted(all_dates['date'])[start] <= seller_distribution.date]
- seller_distribution = seller_distribution[seller_distribution.date <= sorted(all_dates['date'])[finish]]
- seller_distribution = seller_distribution[seller_distribution['order_status'].isin(statuses_filter)]
- if state_to_display != 'All':
- seller_distribution = seller_distribution.loc[seller_distribution['seller_state'] == state_to_display]
- else:
- seller_distribution['seller_state'] = 'All states'
- seller_distribution = seller_distribution.drop(columns=['date'])
- seller_distribution = seller_distribution.groupby(['category', 'seller_state']).agg({'quantity' : 'sum'}).reset_index()
- overall = sum(seller_distribution['quantity'])
- seller_distribution['percentage'] = 100 * seller_distribution['quantity'] / overall
- df = seller_distribution[['category', 'percentage', 'seller_state']]
- fig = px.bar(
- df, y="seller_state", x="percentage", color="category", color_continuous_scale="emrld",
- labels={"seller_state": "Seller State"},
- title="Distribution by Sales Categories",
- orientation='h',
- height=300
- )
- #fig = px.bar(df, y="seller_state", x="percentage", color="category", title="Distribution by sales categories",
- #orientation='h', height=300)
- fig.update_layout(transition_duration=500)
- return fig
- if __name__ == '__main__':
- app.run_server(debug=True, port=8095)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement