File size: 3,375 Bytes
f8ab25d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
import plotly.express as px



df = pd.read_csv(r'FY2021_merged_file.csv', dtype={"Fiscal Week": "string", 
                                            "Fiscal Year": "category", 
                                            "Chain Code": "category", 
                                            "Store": "category", 
                                            "Address": "string", 
                                            "Postal Code": "float", 
                                            "City": "category", 
                                            "State": "category", 
                                            "Container Code": "category", 
                                            "Sales Item Category": "category", 
                                            "units sold":"float", 
                                            "SalePrice":"float", 
                                            "sales $":"float"})

df["Postal Code"] = df["Postal Code"].convert_dtypes()
df["units sold"] = df["units sold"].convert_dtypes()

# Extract fiscal year and week from the 'Fiscal Week' column for sorting
df['Fiscal Year'] = df['Fiscal Week'].apply(lambda x: int(x.split(' ')[1]))  # Extract year as an integer
df['Week Number'] = df['Fiscal Week'].apply(lambda x: int(x.split('Week ')[1]))  # Extract week as an integer

# Sort the DataFrame by fiscal year and week number
df = df.sort_values(by=['Fiscal Year', 'Week Number'])

# Reformat 'Fiscal Week' for display (e.g., 'FY21W51')
df['Fiscal Week Short'] = df.apply(lambda x: f"FY{x['Fiscal Year']%100}W{x['Week Number']}", axis=1)

# Ensure the short fiscal week column is treated as a categorical variable and sorted by the order of appearance
df['Fiscal Week Short'] = pd.Categorical(df['Fiscal Week Short'], categories=df['Fiscal Week Short'].unique(), ordered=True)


# df['Fiscal Week'] = df['Fiscal Week'].apply(lambda x: x.replace('FY 20', 'FY').replace('Week ', 'W'))

# Sort by 'Fiscal Week'
# df = df.sort_values(by='Fiscal Week')

st.title('Sales Data Dashboard')


state = st.selectbox('Select State', df['State'].unique())
feature = st.selectbox('Select Feature for Grouping', ['Chain Code', 'Sales Item Category', 'Fiscal Week'])

# Filter the dataframe based on selections
filtered_df = df[df['State'] == state]

# Plot based on user's selection
if feature == 'Sales Item Category':
    st.subheader(f'Sales Data for {state} - Grouped by Sales Item Category')
    group_data = filtered_df.groupby(['Fiscal Week Short', 'Sales Item Category'])['units sold'].sum().reset_index()
    fig = px.bar(group_data, x='Fiscal Week Short', y='units sold', color='Sales Item Category',
                 title=f'Units Sold over Fiscal Week in {state} by Sales Item Category',
                 labels={'Units Sold': 'Units Sold'})

elif feature == 'Chain Code':
    st.subheader(f'Sales Data for {state} - Grouped by Chain Code')
    group_data = filtered_df.groupby(['Fiscal Week Short', 'Chain Code'])['units sold'].sum().reset_index()
    fig = px.bar(group_data, x='Fiscal Week Short', y='units sold', color='Chain Code',
                 title=f'Units Sold over Fiscal Week in {state} by Chain Code',
                 labels={'Units Sold': 'Units Sold'})

print(df.head(5))
# Display the interactive plot
st.plotly_chart(fig)