-
Notifications
You must be signed in to change notification settings - Fork 1
/
webapp.py
280 lines (204 loc) · 8.23 KB
/
webapp.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import streamlit as st
import pandas as pd
from plotly import express as px
from PIL import Image
# set page configuration
st.set_page_config(
page_title="Analyze It",
page_icon='https://images.emojiterra.com/twitter/v14.0/512px/1f575-2642.png',
)
container = st.container()
col1,col2 = st.columns(2)
@st.cache_data
def load_data(file):
return pd.read_csv(file)
# Functions for Filtering section ...........
# to print top N rows
def top_n_rows(df):
st.sidebar.header('data filtering'.upper())
nrows = st.sidebar.number_input("Top N Rows", min_value=5, max_value=df.shape[0])
return nrows
# to choose only selected columns
def display_columns(df):
columns = st.sidebar.multiselect("Choose Columns:", df.columns)
return columns
# sorting data by user's choice
def sort_data(df):
cols = [None]
for colns in df.columns:
cols.append(colns)
sort_column = st.sidebar.selectbox("Sort by", cols)
order = st.sidebar.selectbox('Order', ['Ascending', 'Descending'])
return sort_column, order
# Functions for Grouping Section ..........
aggregate_functions = ['count', 'sum', 'mean', 'median', 'min', 'max', 'std', 'var']
def group_by(df):
st.sidebar.header('data grouping'.upper())
grouped_column = st.sidebar.selectbox("Column to Group by ", df.columns)
aggregate = st.sidebar.multiselect('Aggregate By ', aggregate_functions)
return grouped_column, aggregate
def sel_colns(df):
colns = st.sidebar.multiselect('Choose Columns: ', df.columns)
return colns
def top_n_grps(grp_data):
total_grps = grp_data.ngroups
mn_value = 5 if total_grps >= 5 else total_grps
ngrps= st.sidebar.number_input("Top N Groups", min_value=mn_value, max_value=total_grps)
return ngrps
def get_grps(grp_data):
grps = [None]
for name, info in grp_data:
grps.append(name)
grps = st.sidebar.selectbox("Choose Group ", grps)
return grps
def analyze_data(data):
# Perform basic data analysis
container.markdown("# DATA ANALYSIS .....")
container.markdown('## ')
with col1:
st.write("### Number of rows:", data.shape[0])
with col2:
st.write("### Number of columns:", data.shape[1])
st.empty()
with col1:
st.write("Columns Names ", data.columns)
with col1:
st.write("Columns Data Types ", data.dtypes)
with col2:
st.write("Missing Values ", data.isnull().sum())
with col2:
st.write("Unique Values ", data.nunique())
try:
# Code for Filtering section
container.markdown('### Basic Analysis of your Data 🕵️')
nrows = top_n_rows(data)
columns = display_columns(data)
sorted_coln, order = sort_data(data)
if len(columns) > 0:
if sorted_coln is not None:
if order == 'Ascending':
data2 = data.sort_values(by=sorted_coln)
container.write(data2[columns].head(nrows))
else:
data2 = data.sort_values(by=sorted_coln, ascending=False)
container.write(data2[columns].head(nrows))
else:
container.write(data[columns].head(nrows))
else:
if sorted_coln is not None:
if order == 'Ascending':
data1 = data.sort_values(by=sorted_coln)
container.write(data1.head(nrows))
else:
data1 = data.sort_values(by=sorted_coln, ascending=False)
container.write(data1.head(nrows))
else:
container.write(data.head(nrows))
# Code for Grouping section
container.markdown('### Analyze your Data by grouping 🕵️')
grp_col, agg_fns = group_by(data)
grp_data = data.groupby(grp_col)
colns = sel_colns(data)
get_grp = get_grps(grp_data)
ngrps = top_n_grps(grp_data=grp_data)
if len(colns) > 0:
if get_grp is not None:
grp_df = grp_data.get_group(get_grp)
container.write(get_grp)
container.write(grp_df[colns].agg(agg_fns))
else:
container.write(grp_data[colns].agg(agg_fns).head(ngrps))
else:
if get_grp is not None:
grp_df = grp_data.get_group(get_grp)
container.write(get_grp)
container.write(grp_df.agg(agg_fns))
else:
container.write(grp_data.agg(agg_fns).head(ngrps))
# Descriptive Analysis
container.write('### Descriptive Analysis')
container.write('Description')
container.write(data.describe())
container.write('Correletion')
container.write(data.corr())
except Exception as error:
# container.write(error)
pass
# code for this function completes here
# FUNCTIONS FOR DATA VISUALIZATION SECTION
def top_n_sections(data):
st.sidebar.header('data filtering'.upper())
nsections= st.sidebar.number_input("Top N Sections", min_value=5, max_value=data.shape[0])
return nsections
def sort_by2(data):
cols = [None]
for colns in data.columns:
cols.append(colns)
sort_column = st.sidebar.selectbox("Sort by", cols)
order = st.sidebar.selectbox('Order', ['Ascending', 'Descending'])
return sort_column, order
def display_chart(data, chart, x_axis, y_axis, xlbl, ylbl):
# creating charts
if chart == 'Bar':
fig = px.bar(data, x=x_axis, y=y_axis, title=f'{xlbl} by {ylbl}', text=y_axis,
labels={'x': xlbl, 'y': ylbl})
fig.update_traces(textposition='outside')
st.plotly_chart(fig)
elif chart == 'Line':
fig = px.line(data, x_axis, y_axis, title=f'{xlbl} by {ylbl}', labels={'x': xlbl, 'y': ylbl})
st.plotly_chart(fig)
elif chart == 'Scatter':
fig = px.scatter(data, x_axis, y_axis, title=f'{xlbl} by {ylbl}', labels={'x': xlbl, 'y': ylbl},
color=y_axis, size=y_axis)
st.plotly_chart(fig)
elif chart == 'Histogram':
fig = px.histogram(data, x_axis, y_axis, title=f'{xlbl} by {ylbl}', log_x=False, log_y=False,
labels={'x': xlbl, 'y': ylbl})
st.plotly_chart(fig)
elif chart == 'Pie':
fig = px.pie(data, names=x_axis, values=y_axis, color=x_axis, title=f'{xlbl} by {ylbl}',
color_discrete_sequence=px.colors.sequential.RdBu, hole=0.5)
st.plotly_chart(fig)
def visualize_data(data):
# Doing Visualization on data
container.markdown("# DATA VISUALIZATION 📈 📊")
top_n_sect = top_n_sections(data)
sorted_coln, order = sort_by2(data)
st.sidebar.header('Data Visualization')
chart_type = st.sidebar.selectbox("Chart Type", ["Bar", "Line", "Scatter", "Histogram","Pie"])
x_column = st.sidebar.selectbox("Label", data.columns)
y_column = st.sidebar.selectbox("Values", data.columns)
try:
if sorted_coln is not None:
if order == 'Ascending':
data2 = data.sort_values(by=sorted_coln)
else:
data2 = data.sort_values(by=sorted_coln, ascending=False)
else:
data2 = data
x_axis = data2[x_column].head(top_n_sect)
y_axis = data2[y_column].head(top_n_sect)
display_chart(data2, chart_type, x_axis, y_axis, x_column, y_column)
except Exception as error:
print(error)
def main():
image = Image.open("banner.png")
# container.image(image,width = 800)
st.sidebar.image(image,width = 300)
file = st.sidebar.file_uploader("Upload a file (CSV Only)", type=['csv'])
options = st.sidebar.radio('pages'.upper(), options = ['Data Analysis','Data visualization'])
if file is not None:
data = load_data(file)
if options == 'Data Analysis':
analyze_data(data)
if options =='Data visualization':
visualize_data(data)
if file is None:
data = pd.read_csv('2016.csv')
if options == 'Data Analysis':
analyze_data(data)
if options == 'Data visualization':
visualize_data(data)
# main function
if __name__ == "__main__":
main()