ProtonDataLabs commited on
Commit
1d3a4de
1 Parent(s): 03e6b4e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -0
app.py CHANGED
@@ -326,3 +326,82 @@ if st.session_state['active_card'] == 'card3':
326
  )
327
 
328
  st.plotly_chart(fig, use_container_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
326
  )
327
 
328
  st.plotly_chart(fig, use_container_width=True)
329
+
330
+ #################### CARD-3 MONTHLY IMPLEMENTATION #########################
331
+ # Ensure 'Dt' column is in datetime format
332
+ df['Dt'] = pd.to_datetime(df['Dt'])
333
+
334
+ # Extract fiscal year and month from 'Dt' column
335
+ df['FY'] = df['Dt'].dt.year.astype(str)
336
+ df['Month'] = df['Dt'].dt.month.astype(str)
337
+
338
+ # Create FY_Month column
339
+ df['FY_Month'] = df['FY'] + '_' + df['Month']
340
+
341
+ # Filter data based on selected item type and selected regions
342
+ filtered_df = df[(df['Itemtype'] == selected_item_type) & (df['Region'].isin(selected_regions))]
343
+
344
+ # Group by Year, Region, Itemtype and aggregate SalesVolume and UnitPrice
345
+ agg_df = filtered_df.groupby(['FY_Month', 'Region', 'Itemtype']).agg({
346
+ 'SalesVolume': 'sum',
347
+ 'UnitPrice': 'mean'
348
+ }).reset_index()
349
+
350
+ # Split FY_Month again for correct sorting
351
+ agg_df[['FY', 'Month']] = agg_df['FY_Month'].str.split('_', expand=True)
352
+ agg_df['Month'] = agg_df['Month'].astype(int)
353
+ agg_df['FY'] = agg_df['FY'].astype(int)
354
+
355
+ # Combine FY and Month back into a datetime-like format for proper sorting
356
+ agg_df['FY_Month_dt'] = pd.to_datetime(agg_df['FY'].astype(str) + agg_df['Month'].astype(str).str.zfill(2), format='%Y%m')
357
+
358
+ # Sort values by Region, Itemtype, and FY_Month_dt
359
+ agg_df = agg_df.sort_values(by=['Region', 'Itemtype', 'FY_Month_dt'])
360
+
361
+ # Calculate YOY percentage changes in Sales Volume and Unit Price
362
+ agg_df['SalesVolume_pct_change'] = agg_df.groupby(['Region', 'Itemtype'])['SalesVolume'].pct_change().round(3) * 100
363
+ agg_df['UnitPrice_pct_change'] = agg_df.groupby(['Region', 'Itemtype'])['UnitPrice'].pct_change().round(3) * 100
364
+
365
+ # Calculate Price Elasticity Coefficient (PE)
366
+ agg_df['PE_Coeff'] = (agg_df['SalesVolume_pct_change'] / agg_df['UnitPrice_pct_change']).round(2)
367
+
368
+ # Exclude FY 2021 and FY 2025
369
+ agg_df_filtered = agg_df[~agg_df['FY'].astype(str).str.contains('2020|2021|2025')]
370
+
371
+ # Drop rows where PE_Coeff is NaN (optional)
372
+ agg_df_filtered = agg_df_filtered.dropna(subset=['PE_Coeff'])
373
+ agg_df_filtered = agg_df_filtered[(agg_df_filtered['PE_Coeff'] < 1000) & (agg_df_filtered['PE_Coeff'] > -1000)]
374
+
375
+ # Plot the PE Coefficient with Plotly
376
+ fig = go.Figure()
377
+
378
+ # Iterate through each selected region and plot separately
379
+ for region in selected_regions:
380
+ # Filter the DataFrame for the current region
381
+ region_df = agg_df_filtered[agg_df_filtered['Region'] == region]
382
+
383
+ # Add a line trace for the region
384
+ fig.add_trace(go.Scatter(
385
+ x=region_df['FY_Month_dt'], # Use the datetime-like column for correct sorting
386
+ y=region_df['PE_Coeff'],
387
+ mode='lines+markers',
388
+ name=region, # Set the name to the region to appear in the legend
389
+ line=dict(width=2),
390
+ marker=dict(size=6),
391
+ ))
392
+
393
+ # Customize layout
394
+ fig.update_layout(
395
+ title=f"Price Elasticity Coefficient (PE) by Year-Month for {selected_item_type}",
396
+ xaxis_title="Fiscal Year_Month",
397
+ yaxis_title="Price Elasticity Coefficient (PE)",
398
+ height=600,
399
+ width=1000,
400
+ legend_title="Region",
401
+ xaxis=dict(
402
+ tickformat='%Y-%m', # Format X-axis ticks as Year-Month
403
+ )
404
+ )
405
+
406
+ # Show the plot in Streamlit
407
+ st.plotly_chart(fig, use_container_width=True)