ProtonDataLabs commited on
Commit
9e3c2c5
1 Parent(s): f0ca619

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -21
app.py CHANGED
@@ -117,11 +117,14 @@ if st.session_state['active_card'] == 'card1':
117
  # Card 2: Sales Volume & Unit Price Correlation plot for Item Category and Container Code
118
  if st.session_state['active_card'] == 'card2':
119
  # Dropdown to select item type (using session_state)
120
- st.session_state['selected_item_type'] = st.selectbox('Select Item Type',
121
- df['Itemtype'].unique(),
122
- index=list(df['Itemtype'].unique()).index(st.session_state['selected_item_type']))
123
 
124
- st.subheader("Sales Volume & Unit Price Correlation for Container Codes")
 
 
 
125
 
126
  # Group the dataframe and prepare for plotting
127
  df = df.groupby(['FyWeek', 'Fy', 'Chaincode', 'Store', 'Address', 'Zipcode', 'City', 'State', 'Containercode', 'Itemtype'], observed=True).agg({
@@ -149,42 +152,42 @@ if st.session_state['active_card'] == 'card2':
149
  # Filter the dataframe based on the selected item type
150
  filtered_df = df[df['Itemtype'] == st.session_state['selected_item_type']]
151
 
152
- # Find the top 3 container codes based on total SalesVolume
153
- top_3_containers = filtered_df.groupby('Containercode', observed=True)['SalesVolume'].sum().nlargest(3).index
154
 
155
- # Filter the data for only the top 3 container codes
156
- top_container_data = filtered_df[filtered_df['Containercode'].isin(top_3_containers)]
157
 
158
- # Group by Year, Week, Dt, and Containercode and aggregate SalesVolume and UnitPrice
159
- agg_df = top_container_data.groupby(['Containercode', 'Year', 'Week', 'Dt'], observed=True).agg({
160
  'SalesVolume': 'sum',
161
  'UnitPrice': 'mean'
162
  }).reset_index()
163
 
164
- # Loop through the top 3 container codes and create separate plots
165
- for container in top_3_containers:
166
- container_data = agg_df[agg_df['Containercode'] == container]
167
 
168
- # Create a new figure for each container code
169
  fig, (axd, axp) = plt.subplots(2, 1, figsize=(10, 6))
170
 
171
  # Plot SalesVolume
172
- sns.lineplot(data=container_data, x='Dt', y='SalesVolume', ax=axd)
173
- axd.set_title(f"SalesVolume - {container}")
174
  axd.grid(True, linestyle='--', color='gray', alpha=0.7)
175
 
176
  # Plot mean line for SalesVolume
177
- axd.axhline(container_data['SalesVolume'].mean(), ls="--", color="r")
178
  axd.xaxis.set_major_locator(mdates.MonthLocator(interval=2))
179
  axd.set_xticklabels([])
180
 
181
  # Plot UnitPrice
182
- sns.lineplot(data=container_data, x='Dt', y='UnitPrice', ax=axp, color='green', errorbar='sd')
183
- axp.set_title(f"UnitPrice - {container}")
184
  axp.grid(True, linestyle='--', color='gray', alpha=0.7)
185
 
186
  # Plot mean line for UnitPrice
187
- axp.axhline(container_data['UnitPrice'].mean(), ls="--", color="r")
188
  axp.xaxis.set_major_locator(mdates.MonthLocator(interval=2))
189
  axp.tick_params(axis='x', rotation=90)
190
 
@@ -193,7 +196,6 @@ if st.session_state['active_card'] == 'card2':
193
 
194
  # Display the plot in Streamlit
195
  st.pyplot(fig)
196
-
197
  ###############################################################################################
198
 
199
  ########################################### CARD #3 ####################################################
 
117
  # Card 2: Sales Volume & Unit Price Correlation plot for Item Category and Container Code
118
  if st.session_state['active_card'] == 'card2':
119
  # Dropdown to select item type (using session_state)
120
+ st.session_state['selected_item_type'] = st.selectbox(
121
+ 'Select Item Type', df['Itemtype'].unique(),
122
+ index=list(df['Itemtype'].unique()).index(st.session_state['selected_item_type']))
123
 
124
+ # Dropdown to select the grouping category (container code, chain code, or state)
125
+ group_by_option = st.selectbox('Group by', ['Containercode', 'Chaincode', 'State'])
126
+
127
+ st.subheader(f"Sales Volume & Unit Price Correlation for {group_by_option}")
128
 
129
  # Group the dataframe and prepare for plotting
130
  df = df.groupby(['FyWeek', 'Fy', 'Chaincode', 'Store', 'Address', 'Zipcode', 'City', 'State', 'Containercode', 'Itemtype'], observed=True).agg({
 
152
  # Filter the dataframe based on the selected item type
153
  filtered_df = df[df['Itemtype'] == st.session_state['selected_item_type']]
154
 
155
+ # Find the top 3 values based on total SalesVolume in the selected grouping category
156
+ top_3_values = filtered_df.groupby(group_by_option, observed=True)['SalesVolume'].sum().nlargest(3).index
157
 
158
+ # Filter the data for only the top 3 values
159
+ top_group_data = filtered_df[filtered_df[group_by_option].isin(top_3_values)]
160
 
161
+ # Group by Year, Week, Dt, and the selected category and aggregate SalesVolume and UnitPrice
162
+ agg_df = top_group_data.groupby([group_by_option, 'Year', 'Week', 'Dt'], observed=True).agg({
163
  'SalesVolume': 'sum',
164
  'UnitPrice': 'mean'
165
  }).reset_index()
166
 
167
+ # Loop through the top 3 values and create separate plots
168
+ for value in top_3_values:
169
+ value_data = agg_df[agg_df[group_by_option] == value]
170
 
171
+ # Create a new figure for each group
172
  fig, (axd, axp) = plt.subplots(2, 1, figsize=(10, 6))
173
 
174
  # Plot SalesVolume
175
+ sns.lineplot(data=value_data, x='Dt', y='SalesVolume', ax=axd)
176
+ axd.set_title(f"SalesVolume - {value} ({group_by_option})")
177
  axd.grid(True, linestyle='--', color='gray', alpha=0.7)
178
 
179
  # Plot mean line for SalesVolume
180
+ axd.axhline(value_data['SalesVolume'].mean(), ls="--", color="r")
181
  axd.xaxis.set_major_locator(mdates.MonthLocator(interval=2))
182
  axd.set_xticklabels([])
183
 
184
  # Plot UnitPrice
185
+ sns.lineplot(data=value_data, x='Dt', y='UnitPrice', ax=axp, color='green', errorbar='sd')
186
+ axp.set_title(f"UnitPrice - {value} ({group_by_option})")
187
  axp.grid(True, linestyle='--', color='gray', alpha=0.7)
188
 
189
  # Plot mean line for UnitPrice
190
+ axp.axhline(value_data['UnitPrice'].mean(), ls="--", color="r")
191
  axp.xaxis.set_major_locator(mdates.MonthLocator(interval=2))
192
  axp.tick_params(axis='x', rotation=90)
193
 
 
196
 
197
  # Display the plot in Streamlit
198
  st.pyplot(fig)
 
199
  ###############################################################################################
200
 
201
  ########################################### CARD #3 ####################################################