humanist96 commited on
Commit
de9d51f
·
1 Parent(s): 8dc14cf

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +160 -0
app.py ADDED
@@ -0,0 +1,160 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import plotly.express as px
3
+ import pandas as pd
4
+ import os
5
+ import warnings
6
+ warnings.filterwarnings('ignore')
7
+
8
+ st.set_page_config(page_title="Superstore!!!", page_icon=":bar_chart:",layout="wide")
9
+
10
+ st.title(" :bar_chart: Sample SuperStore EDA")
11
+ st.markdown('<style>div.block-container{padding-top:1rem;}</style>',unsafe_allow_html=True)
12
+
13
+ fl = st.file_uploader(":file_folder: Upload a file",type=(["csv","txt","xlsx","xls"]))
14
+ if fl is not None:
15
+ filename = fl.name
16
+ st.write(filename)
17
+ df = pd.read_csv(filename, encoding = "ISO-8859-1")
18
+ else:
19
+ os.chdir(r"C:\Users\AEPAC\Desktop\Streamlit")
20
+ df = pd.read_csv("Superstore.csv", encoding = "ISO-8859-1")
21
+
22
+ col1, col2 = st.columns((2))
23
+ df["Order Date"] = pd.to_datetime(df["Order Date"])
24
+
25
+ # Getting the min and max date
26
+ startDate = pd.to_datetime(df["Order Date"]).min()
27
+ endDate = pd.to_datetime(df["Order Date"]).max()
28
+
29
+ with col1:
30
+ date1 = pd.to_datetime(st.date_input("Start Date", startDate))
31
+
32
+ with col2:
33
+ date2 = pd.to_datetime(st.date_input("End Date", endDate))
34
+
35
+ df = df[(df["Order Date"] >= date1) & (df["Order Date"] <= date2)].copy()
36
+
37
+ st.sidebar.header("Choose your filter: ")
38
+ # Create for Region
39
+ region = st.sidebar.multiselect("Pick your Region", df["Region"].unique())
40
+ if not region:
41
+ df2 = df.copy()
42
+ else:
43
+ df2 = df[df["Region"].isin(region)]
44
+
45
+ # Create for State
46
+ state = st.sidebar.multiselect("Pick the State", df2["State"].unique())
47
+ if not state:
48
+ df3 = df2.copy()
49
+ else:
50
+ df3 = df2[df2["State"].isin(state)]
51
+
52
+ # Create for City
53
+ city = st.sidebar.multiselect("Pick the City",df3["City"].unique())
54
+
55
+ # Filter the data based on Region, State and City
56
+
57
+ if not region and not state and not city:
58
+ filtered_df = df
59
+ elif not state and not city:
60
+ filtered_df = df[df["Region"].isin(region)]
61
+ elif not region and not city:
62
+ filtered_df = df[df["State"].isin(state)]
63
+ elif state and city:
64
+ filtered_df = df3[df["State"].isin(state) & df3["City"].isin(city)]
65
+ elif region and city:
66
+ filtered_df = df3[df["Region"].isin(region) & df3["City"].isin(city)]
67
+ elif region and state:
68
+ filtered_df = df3[df["Region"].isin(region) & df3["State"].isin(state)]
69
+ elif city:
70
+ filtered_df = df3[df3["City"].isin(city)]
71
+ else:
72
+ filtered_df = df3[df3["Region"].isin(region) & df3["State"].isin(state) & df3["City"].isin(city)]
73
+
74
+ category_df = filtered_df.groupby(by = ["Category"], as_index = False)["Sales"].sum()
75
+
76
+ with col1:
77
+ st.subheader("Category wise Sales")
78
+ fig = px.bar(category_df, x = "Category", y = "Sales", text = ['${:,.2f}'.format(x) for x in category_df["Sales"]],
79
+ template = "seaborn")
80
+ st.plotly_chart(fig,use_container_width=True, height = 200)
81
+
82
+ with col2:
83
+ st.subheader("Region wise Sales")
84
+ fig = px.pie(filtered_df, values = "Sales", names = "Region", hole = 0.5)
85
+ fig.update_traces(text = filtered_df["Region"], textposition = "outside")
86
+ st.plotly_chart(fig,use_container_width=True)
87
+
88
+ cl1, cl2 = st.columns((2))
89
+ with cl1:
90
+ with st.expander("Category_ViewData"):
91
+ st.write(category_df.style.background_gradient(cmap="Blues"))
92
+ csv = category_df.to_csv(index = False).encode('utf-8')
93
+ st.download_button("Download Data", data = csv, file_name = "Category.csv", mime = "text/csv",
94
+ help = 'Click here to download the data as a CSV file')
95
+
96
+ with cl2:
97
+ with st.expander("Region_ViewData"):
98
+ region = filtered_df.groupby(by = "Region", as_index = False)["Sales"].sum()
99
+ st.write(region.style.background_gradient(cmap="Oranges"))
100
+ csv = region.to_csv(index = False).encode('utf-8')
101
+ st.download_button("Download Data", data = csv, file_name = "Region.csv", mime = "text/csv",
102
+ help = 'Click here to download the data as a CSV file')
103
+
104
+ filtered_df["month_year"] = filtered_df["Order Date"].dt.to_period("M")
105
+ st.subheader('Time Series Analysis')
106
+
107
+ linechart = pd.DataFrame(filtered_df.groupby(filtered_df["month_year"].dt.strftime("%Y : %b"))["Sales"].sum()).reset_index()
108
+ fig2 = px.line(linechart, x = "month_year", y="Sales", labels = {"Sales": "Amount"},height=500, width = 1000,template="gridon")
109
+ st.plotly_chart(fig2,use_container_width=True)
110
+
111
+ with st.expander("View Data of TimeSeries:"):
112
+ st.write(linechart.T.style.background_gradient(cmap="Blues"))
113
+ csv = linechart.to_csv(index=False).encode("utf-8")
114
+ st.download_button('Download Data', data = csv, file_name = "TimeSeries.csv", mime ='text/csv')
115
+
116
+ # Create a treem based on Region, category, sub-Category
117
+ st.subheader("Hierarchical view of Sales using TreeMap")
118
+ fig3 = px.treemap(filtered_df, path = ["Region","Category","Sub-Category"], values = "Sales",hover_data = ["Sales"],
119
+ color = "Sub-Category")
120
+ fig3.update_layout(width = 800, height = 650)
121
+ st.plotly_chart(fig3, use_container_width=True)
122
+
123
+ chart1, chart2 = st.columns((2))
124
+ with chart1:
125
+ st.subheader('Segment wise Sales')
126
+ fig = px.pie(filtered_df, values = "Sales", names = "Segment", template = "plotly_dark")
127
+ fig.update_traces(text = filtered_df["Segment"], textposition = "inside")
128
+ st.plotly_chart(fig,use_container_width=True)
129
+
130
+ with chart2:
131
+ st.subheader('Category wise Sales')
132
+ fig = px.pie(filtered_df, values = "Sales", names = "Category", template = "gridon")
133
+ fig.update_traces(text = filtered_df["Category"], textposition = "inside")
134
+ st.plotly_chart(fig,use_container_width=True)
135
+
136
+ import plotly.figure_factory as ff
137
+ st.subheader(":point_right: Month wise Sub-Category Sales Summary")
138
+ with st.expander("Summary_Table"):
139
+ df_sample = df[0:5][["Region","State","City","Category","Sales","Profit","Quantity"]]
140
+ fig = ff.create_table(df_sample, colorscale = "Cividis")
141
+ st.plotly_chart(fig, use_container_width=True)
142
+
143
+ st.markdown("Month wise sub-Category Table")
144
+ filtered_df["month"] = filtered_df["Order Date"].dt.month_name()
145
+ sub_category_Year = pd.pivot_table(data = filtered_df, values = "Sales", index = ["Sub-Category"],columns = "month")
146
+ st.write(sub_category_Year.style.background_gradient(cmap="Blues"))
147
+
148
+ # Create a scatter plot
149
+ data1 = px.scatter(filtered_df, x = "Sales", y = "Profit", size = "Quantity")
150
+ data1['layout'].update(title="Relationship between Sales and Profits using Scatter Plot.",
151
+ titlefont = dict(size=20),xaxis = dict(title="Sales",titlefont=dict(size=19)),
152
+ yaxis = dict(title = "Profit", titlefont = dict(size=19)))
153
+ st.plotly_chart(data1,use_container_width=True)
154
+
155
+ with st.expander("View Data"):
156
+ st.write(filtered_df.iloc[:500,1:20:2].style.background_gradient(cmap="Oranges"))
157
+
158
+ # Download orginal DataSet
159
+ csv = df.to_csv(index = False).encode('utf-8')
160
+ st.download_button('Download Data', data = csv, file_name = "Data.csv",mime = "text/csv")