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aned_data.csv",parse_dates=['Order Date'],index_col='Order Date')
|
| 8 |
+
df_train = df.index< '2018-01-01'
|
| 9 |
+
|
| 10 |
+
df_test = df.index>= '2018-01-01'
|
| 11 |
+
df_train = df[df_train]
|
| 12 |
+
df_test = df[df_test]
|
| 13 |
+
time_pred = ["Past","Future"]
|
| 14 |
+
|
| 15 |
+
#display the years of data as a slider 2015-2017 for past and 2018 for future
|
| 16 |
+
|
| 17 |
+
k = st.sidebar.selectbox("Time",time_pred)
|
| 18 |
+
if k == "Past":
|
| 19 |
+
n_years = st.sidebar.slider("Years of data", 2015, 2016, 2017)
|
| 20 |
+
|
| 21 |
+
periods = 12*n_years
|
| 22 |
+
else:
|
| 23 |
+
n_years = st.sidebar.slider("Years of data", 2018,2019)
|
| 24 |
+
periods = 12
|
| 25 |
+
|
| 26 |
+
@st.cache_data
|
| 27 |
+
def load_data():
|
| 28 |
+
data = df.copy()
|
| 29 |
+
|
| 30 |
+
return data
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
data_load_state = st.text("Loading data...")
|
| 34 |
+
data = load_data()
|
| 35 |
+
data_load_state.text("Loading data...done!")
|
| 36 |
+
|
| 37 |
+
st.subheader("Raw data")
|
| 38 |
+
st.write(data.head())
|
| 39 |
+
|
| 40 |
+
def plot_raw_data_year(input:str):
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
if input == "Past":
|
| 44 |
+
|
| 45 |
+
df_yearly= df_train.groupby(pd.Grouper(freq='Y'))['Sales'].sum()
|
| 46 |
+
df_yearly = pd.DataFrame(df_yearly)
|
| 47 |
+
else:
|
| 48 |
+
df_yearly = df_test.groupby(pd.Grouper(freq='Y'))['Sales'].sum()
|
| 49 |
+
df_yearly = pd.DataFrame(df_yearly)
|
| 50 |
+
|
| 51 |
+
fig = go.Figure()
|
| 52 |
+
fig.add_trace(go.Bar(x=df_yearly.index, y=df_yearly.Sales,name='Yearly Sales' ,))
|
| 53 |
+
fig.update_layout(title_text='Yearly Sales',plot_bgcolor='white',xaxis_rangeslider_visible=True)
|
| 54 |
+
st.plotly_chart(fig)
|
| 55 |
+
|
| 56 |
+
plot_raw_data_year(k)
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def plot_raw_data_month(input:str):
|
| 60 |
+
if input == "Past":
|
| 61 |
+
df_monthly= df_train.groupby(pd.Grouper(freq='M'))['Sales'].sum()
|
| 62 |
+
df_monthly = pd.DataFrame(df_monthly)
|
| 63 |
+
else:
|
| 64 |
+
df_monthly = df_test.groupby(pd.Grouper(freq='M'))['Sales'].sum()
|
| 65 |
+
df_monthly = pd.DataFrame(df_monthly)
|
| 66 |
+
|
| 67 |
+
fig = go.Figure()
|
| 68 |
+
fig.add_trace(go.Scatter(x=df_monthly.index, y=df_monthly.Sales,name='Monthly Sales' ))
|
| 69 |
+
fig.update_layout(title_text= 'Monthly Sales',plot_bgcolor='white',xaxis_rangeslider_visible=True)
|
| 70 |
+
st.plotly_chart(fig)
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
plot_raw_data_month(k)
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def plot_raw_data_day(input:str):
|
| 77 |
+
if input == "Past":
|
| 78 |
+
df_daily= df_train.groupby(pd.Grouper(freq='D'))['Sales'].sum()
|
| 79 |
+
df_daily = pd.DataFrame(df_daily)
|
| 80 |
+
else:
|
| 81 |
+
df_daily = df_test.groupby(pd.Grouper(freq='D'))['Sales'].sum()
|
| 82 |
+
df_daily = pd.DataFrame(df_daily)
|
| 83 |
+
|
| 84 |
+
fig = go.Figure()
|
| 85 |
+
fig.add_trace(go.Scatter(x=df_daily.index, y=df_daily.Sales,name='Daily Sales' ))
|
| 86 |
+
fig.update_layout(title_text= 'Daily Sales',plot_bgcolor='white',xaxis_rangeslider_visible=True)
|
| 87 |
+
st.plotly_chart(fig)
|
| 88 |
+
|
| 89 |
+
plot_raw_data_day(k)
|
| 90 |
+
|
| 91 |
+
def plot_raw_yearly_sales_by_segment(input:str):
|
| 92 |
+
|
| 93 |
+
if input == "Past":
|
| 94 |
+
df_yearly_segment = df_train.groupby([pd.Grouper(freq='Y'), 'Segment'])['Sales'].sum().reset_index()
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
df_yearly_segment = pd.DataFrame(df_yearly_segment)
|
| 98 |
+
else:
|
| 99 |
+
df_yearly_segment = df_test.groupby([pd.Grouper(freq='Y'), 'Segment'])['Sales'].sum().reset_index()
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
df_yearly_segment = pd.DataFrame(df_yearly_segment)
|
| 103 |
+
color_scale = px.colors.sequential.Viridis
|
| 104 |
+
|
| 105 |
+
# create a dictionary that maps each unique value in the Segment column to a color from the color scheme
|
| 106 |
+
color_map = {segment: color_scale[i % len(color_scale)] for i, segment in enumerate(df_yearly_segment['Segment'].unique())}
|
| 107 |
+
|
| 108 |
+
# use the color_map dictionary to map the Segment values to colors
|
| 109 |
+
colors = df_yearly_segment['Segment'].map(color_map)
|
| 110 |
+
|
| 111 |
+
# create the plot using plotly.graph_objects
|
| 112 |
+
fig = go.Figure(data=go.Bar(x=df_yearly_segment['Order Date'], y=df_yearly_segment['Sales'], marker={'color': colors},hovertext=df_yearly_segment['Segment']))
|
| 113 |
+
fig.update_layout(title_text='Yearly Sales by Segment', plot_bgcolor='white')
|
| 114 |
+
|
| 115 |
+
st.plotly_chart(fig)
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
plot_raw_yearly_sales_by_segment(k)
|
| 119 |
+
def plot_raw_yearly_sales_by_region(input:str):
|
| 120 |
+
|
| 121 |
+
if input == "Past":
|
| 122 |
+
df_yearly_segment = df_train.groupby([pd.Grouper(freq='Y'), 'Region'])['Sales'].sum().reset_index()
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
df_yearly_segment = pd.DataFrame(df_yearly_segment)
|
| 126 |
+
else:
|
| 127 |
+
df_yearly_segment = df_test.groupby([pd.Grouper(freq='Y'), 'Region'])['Sales'].sum().reset_index()
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
df_yearly_segment = pd.DataFrame(df_yearly_segment)
|
| 131 |
+
color_scale = px.colors.sequential.Viridis
|
| 132 |
+
|
| 133 |
+
# create a dictionary that maps each unique value in the Segment column to a color from the color scheme
|
| 134 |
+
color_map = {segment: color_scale[i % len(color_scale)] for i, segment in enumerate(df_yearly_segment['Region'].unique())}
|
| 135 |
+
|
| 136 |
+
# use the color_map dictionary to map the Segment values to colors
|
| 137 |
+
colors = df_yearly_segment['Region'].map(color_map)
|
| 138 |
+
|
| 139 |
+
# create the plot using plotly.graph_objects
|
| 140 |
+
fig = go.Figure(data=go.Bar(x=df_yearly_segment['Order Date'], y=df_yearly_segment['Sales'], marker={'color': colors},hovertext=df_yearly_segment['Region']))
|
| 141 |
+
fig.update_layout(title_text='Yearly Sales by Region', plot_bgcolor='white')
|
| 142 |
+
st.plotly_chart(fig)
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
plot_raw_yearly_sales_by_region(k)
|
| 146 |
+
|
| 147 |
+
def plot_raw_yearly_sales_by_Category(input:str):
|
| 148 |
+
|
| 149 |
+
if input == "Past":
|
| 150 |
+
df_yearly_segment = df_train.groupby([pd.Grouper(freq='Y'), 'Category'])['Sales'].sum().reset_index()
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
else:
|
| 155 |
+
df_yearly_segment = df_test.groupby([pd.Grouper(freq='Y'), 'Category'])['Sales'].sum().reset_index()
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
df_yearly_segment = pd.DataFrame(df_yearly_segment)
|
| 159 |
+
color_scale = px.colors.sequential.Viridis
|
| 160 |
+
|
| 161 |
+
# create a dictionary that maps each unique value in the Segment column to a color from the color scheme
|
| 162 |
+
color_map = {segment: color_scale[i % len(color_scale)] for i, segment in enumerate(df_yearly_segment['Category'].unique())}
|
| 163 |
+
|
| 164 |
+
# use the color_map dictionary to map the Segment values to colors
|
| 165 |
+
colors = df_yearly_segment['Category'].map(color_map)
|
| 166 |
+
|
| 167 |
+
# create the plot using plotly.graph_objects
|
| 168 |
+
fig = go.Figure(data=go.Bar(x=df_yearly_segment['Order Date'], y=df_yearly_segment['Sales'], marker={'color': colors},hovertext=df_yearly_segment['Category']))
|
| 169 |
+
fig.update_layout(title_text='Yearly Sales by Category', plot_bgcolor='white')
|
| 170 |
+
st.plotly_chart(fig)
|
| 171 |
+
|
| 172 |
+
plot_raw_yearly_sales_by_Category(k)
|
| 173 |
+
|
| 174 |
+
def plot_raw_yearly_sales_by_State(input:str, number:int):
|
| 175 |
+
|
| 176 |
+
if input == "Past":
|
| 177 |
+
df_yearly_state = df_train.groupby([pd.Grouper(freq='Y'), 'State'])['Sales'].sum().reset_index()
|
| 178 |
+
else:
|
| 179 |
+
df_yearly_state = df_test.groupby([pd.Grouper(freq='Y'), 'State'])['Sales'].sum().reset_index()
|
| 180 |
+
|
| 181 |
+
df_yearly_state = pd.DataFrame(df_yearly_state)
|
| 182 |
+
color_scale = px.colors.sequential.Viridis
|
| 183 |
+
topN_states = df_yearly_state.groupby('State').sum().sort_values('Sales', ascending=False).head(number).index.tolist()
|
| 184 |
+
top_states_df = df_yearly_state[df_yearly_state['State'].isin(topN_states)]
|
| 185 |
+
|
| 186 |
+
# create a dictionary that maps each unique value in the State column to a color from the color scheme
|
| 187 |
+
color_map = {state: color_scale[i % len(color_scale)] for i, state in enumerate(top_states_df['State'].unique())}
|
| 188 |
+
|
| 189 |
+
# use the color_map dictionary to map the State values to colors
|
| 190 |
+
colors = top_states_df['State'].map(color_map)
|
| 191 |
+
|
| 192 |
+
# create the plot using plotly.graph_objects
|
| 193 |
+
fig = go.Figure(data=go.Bar(x=top_states_df['Order Date'], y=top_states_df['Sales'], marker={'color': colors},hovertext=top_states_df['State']))
|
| 194 |
+
fig.update_layout(title_text=f'Top {number} states with highest sales', plot_bgcolor='white')
|
| 195 |
+
st.plotly_chart(fig)
|
| 196 |
+
|
| 197 |
+
|
| 198 |
+
# initialize Streamlit slider for selecting number of subcategories to display
|
| 199 |
+
number_st = st.slider('Select the number of States', 1, 10, 3)
|
| 200 |
+
|
| 201 |
+
plot_raw_yearly_sales_by_State(k,number_st)
|
| 202 |
+
|
| 203 |
+
def plot_raw_yearly_sales_by_Sub_Cat(input:str, number:int):
|
| 204 |
+
|
| 205 |
+
if input == "Past":
|
| 206 |
+
df_yearly_state = df_train.groupby([pd.Grouper(freq='Y'), 'Sub-Category'])['Sales'].sum().reset_index()
|
| 207 |
+
else:
|
| 208 |
+
df_yearly_state = df_test.groupby([pd.Grouper(freq='Y'), 'Sub-Category'])['Sales'].sum().reset_index()
|
| 209 |
+
|
| 210 |
+
df_yearly_state = pd.DataFrame(df_yearly_state)
|
| 211 |
+
color_scale = px.colors.sequential.Viridis
|
| 212 |
+
topN_states = df_yearly_state.groupby('Sub-Category').sum().sort_values('Sales', ascending=False).head(number).index.tolist()
|
| 213 |
+
top_states_df = df_yearly_state[df_yearly_state['Sub-Category'].isin(topN_states)]
|
| 214 |
+
|
| 215 |
+
# create a dictionary that maps each unique value in the State column to a color from the color scheme
|
| 216 |
+
color_map = {state: color_scale[i % len(color_scale)] for i, state in enumerate(top_states_df['Sub-Category'].unique())}
|
| 217 |
+
|
| 218 |
+
# use the color_map dictionary to map the State values to colors
|
| 219 |
+
colors = top_states_df['Sub-Category'].map(color_map)
|
| 220 |
+
|
| 221 |
+
# create the plot using plotly.graph_objects
|
| 222 |
+
fig = go.Figure(data=go.Bar(x=top_states_df['Order Date'], y=top_states_df['Sub-Category'], marker={'color': colors},hovertext=top_states_df['Sub-Category']))
|
| 223 |
+
fig.update_layout(title_text=f'Top {number} sub categories with highest sales', plot_bgcolor='white')
|
| 224 |
+
st.plotly_chart(fig)
|
| 225 |
+
|
| 226 |
+
|
| 227 |
+
# initialize Streamlit slider for selecting number of subcategories to display
|
| 228 |
+
number_sub_cat = st.slider('Select the number of Sub-Category', 1, 10, 3)
|
| 229 |
+
|
| 230 |
+
plot_raw_yearly_sales_by_Sub_Cat(k,number_sub_cat)
|
| 231 |
+
|
| 232 |
+
|
| 233 |
+
|
| 234 |
+
|
| 235 |
+
|
| 236 |
+
def plot_raw_yearly_sales_by_Product(input:str,number:int):
|
| 237 |
+
|
| 238 |
+
if input == "Past":
|
| 239 |
+
df_yearly_product = df_train.groupby([pd.Grouper(freq='Y'), 'Product Name'])['Sales'].sum().reset_index()
|
| 240 |
+
else:
|
| 241 |
+
df_yearly_product = df_test.groupby([pd.Grouper(freq='Y'), 'Product Name'])['Sales'].sum().reset_index()
|
| 242 |
+
|
| 243 |
+
df_yearly_product = pd.DataFrame(df_yearly_product)
|
| 244 |
+
color_scale = px.colors.sequential.Viridis
|
| 245 |
+
topN_products = df_yearly_product.groupby('Product Name').sum().sort_values('Sales', ascending=False).head(number).index.tolist()
|
| 246 |
+
top_product_df = df_yearly_product[df_yearly_product['Product Name'].isin(topN_products)]
|
| 247 |
+
|
| 248 |
+
# create a dictionary that maps each unique value in the Product Name column to a color from the color scheme
|
| 249 |
+
color_map = {product: color_scale[i % len(color_scale)] for i, product in enumerate(top_product_df['Product Name'].unique())}
|
| 250 |
+
|
| 251 |
+
# use the color_map dictionary to map the Product Name values to colors
|
| 252 |
+
colors = top_product_df['Product Name'].map(color_map)
|
| 253 |
+
|
| 254 |
+
# create the plot using plotly.graph_objects
|
| 255 |
+
fig = go.Figure(data=go.Bar(x=top_product_df['Order Date'], y=top_product_df['Sales'], marker={'color': colors},hovertext=top_product_df['Product Name']))
|
| 256 |
+
fig.update_layout(title_text=f'Top {number} best-selling products', plot_bgcolor='white')
|
| 257 |
+
st.plotly_chart(fig)
|
| 258 |
+
|
| 259 |
+
# initialize Streamlit slider for selecting number of products to display
|
| 260 |
+
number_p = st.slider('Select the number of products to display', 1, 10, 3)
|
| 261 |
+
plot_raw_yearly_sales_by_Product(k,number_p)
|
| 262 |
+
|
| 263 |
+
|
| 264 |
+
def plot_raw_yearly_sales_by_City(input:str, number:int):
|
| 265 |
+
|
| 266 |
+
if input == "Past":
|
| 267 |
+
df_yearly_state = df_train.groupby([pd.Grouper(freq='Y'), 'City'])['Sales'].sum().reset_index()
|
| 268 |
+
else:
|
| 269 |
+
df_yearly_state = df_test.groupby([pd.Grouper(freq='Y'), 'City'])['Sales'].sum().reset_index()
|
| 270 |
+
|
| 271 |
+
df_yearly_state = pd.DataFrame(df_yearly_state)
|
| 272 |
+
color_scale = px.colors.sequential.Viridis
|
| 273 |
+
topN_states = df_yearly_state.groupby('City').sum().sort_values('Sales', ascending=False).head(number).index.tolist()
|
| 274 |
+
top_states_df = df_yearly_state[df_yearly_state['City'].isin(topN_states)]
|
| 275 |
+
|
| 276 |
+
# create a dictionary that maps each unique value in the State column to a color from the color scheme
|
| 277 |
+
color_map = {state: color_scale[i % len(color_scale)] for i, state in enumerate(top_states_df['City'].unique())}
|
| 278 |
+
|
| 279 |
+
# use the color_map dictionary to map the State values to colors
|
| 280 |
+
colors = top_states_df['City'].map(color_map)
|
| 281 |
+
|
| 282 |
+
# create the plot using plotly.graph_objects
|
| 283 |
+
fig = go.Figure(data=go.Bar(x=top_states_df['Order Date'], y=top_states_df['City'], marker={'color': colors},hovertext=top_states_df['City']))
|
| 284 |
+
fig.update_layout(title_text=f'Top {number} states with highest sales', plot_bgcolor='white')
|
| 285 |
+
st.plotly_chart(fig)
|
| 286 |
+
|
| 287 |
+
|
| 288 |
+
# initialize Streamlit slider for selecting number of subcategories to display
|
| 289 |
+
number_city = st.slider('Select the number of Cities', 1, 10, 3)
|
| 290 |
+
|
| 291 |
+
plot_raw_yearly_sales_by_City(k,number_city)
|
| 292 |
+
|
| 293 |
+
|
| 294 |
+
|
| 295 |
+
|
| 296 |
+
|
| 297 |
+
|
| 298 |
+
|
| 299 |
+
|
| 300 |
+
|
| 301 |
+
|
| 302 |
+
|
| 303 |
+
|
| 304 |
+
|
| 305 |
+
|
| 306 |
+
|
| 307 |
+
|