ProtonDataLabs commited on
Commit
f8ab25d
1 Parent(s): 66d9ec5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -0
app.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import matplotlib.pyplot as plt
4
+ import plotly.express as px
5
+
6
+
7
+
8
+ df = pd.read_csv(r'FY2021_merged_file.csv', dtype={"Fiscal Week": "string",
9
+ "Fiscal Year": "category",
10
+ "Chain Code": "category",
11
+ "Store": "category",
12
+ "Address": "string",
13
+ "Postal Code": "float",
14
+ "City": "category",
15
+ "State": "category",
16
+ "Container Code": "category",
17
+ "Sales Item Category": "category",
18
+ "units sold":"float",
19
+ "SalePrice":"float",
20
+ "sales $":"float"})
21
+
22
+ df["Postal Code"] = df["Postal Code"].convert_dtypes()
23
+ df["units sold"] = df["units sold"].convert_dtypes()
24
+
25
+ # Extract fiscal year and week from the 'Fiscal Week' column for sorting
26
+ df['Fiscal Year'] = df['Fiscal Week'].apply(lambda x: int(x.split(' ')[1])) # Extract year as an integer
27
+ df['Week Number'] = df['Fiscal Week'].apply(lambda x: int(x.split('Week ')[1])) # Extract week as an integer
28
+
29
+ # Sort the DataFrame by fiscal year and week number
30
+ df = df.sort_values(by=['Fiscal Year', 'Week Number'])
31
+
32
+ # Reformat 'Fiscal Week' for display (e.g., 'FY21W51')
33
+ df['Fiscal Week Short'] = df.apply(lambda x: f"FY{x['Fiscal Year']%100}W{x['Week Number']}", axis=1)
34
+
35
+ # Ensure the short fiscal week column is treated as a categorical variable and sorted by the order of appearance
36
+ df['Fiscal Week Short'] = pd.Categorical(df['Fiscal Week Short'], categories=df['Fiscal Week Short'].unique(), ordered=True)
37
+
38
+
39
+ # df['Fiscal Week'] = df['Fiscal Week'].apply(lambda x: x.replace('FY 20', 'FY').replace('Week ', 'W'))
40
+
41
+ # Sort by 'Fiscal Week'
42
+ # df = df.sort_values(by='Fiscal Week')
43
+
44
+ st.title('Sales Data Dashboard')
45
+
46
+
47
+ state = st.selectbox('Select State', df['State'].unique())
48
+ feature = st.selectbox('Select Feature for Grouping', ['Chain Code', 'Sales Item Category', 'Fiscal Week'])
49
+
50
+ # Filter the dataframe based on selections
51
+ filtered_df = df[df['State'] == state]
52
+
53
+ # Plot based on user's selection
54
+ if feature == 'Sales Item Category':
55
+ st.subheader(f'Sales Data for {state} - Grouped by Sales Item Category')
56
+ group_data = filtered_df.groupby(['Fiscal Week Short', 'Sales Item Category'])['units sold'].sum().reset_index()
57
+ fig = px.bar(group_data, x='Fiscal Week Short', y='units sold', color='Sales Item Category',
58
+ title=f'Units Sold over Fiscal Week in {state} by Sales Item Category',
59
+ labels={'Units Sold': 'Units Sold'})
60
+
61
+ elif feature == 'Chain Code':
62
+ st.subheader(f'Sales Data for {state} - Grouped by Chain Code')
63
+ group_data = filtered_df.groupby(['Fiscal Week Short', 'Chain Code'])['units sold'].sum().reset_index()
64
+ fig = px.bar(group_data, x='Fiscal Week Short', y='units sold', color='Chain Code',
65
+ title=f'Units Sold over Fiscal Week in {state} by Chain Code',
66
+ labels={'Units Sold': 'Units Sold'})
67
+
68
+ print(df.head(5))
69
+ # Display the interactive plot
70
+ st.plotly_chart(fig)