abaliyan commited on
Commit
cc26990
·
verified ·
1 Parent(s): ee71b7b

Create utils.py

Browse files
Files changed (1) hide show
  1. utils.py +58 -0
utils.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import numpy as np
3
+ import streamlit as st
4
+
5
+
6
+
7
+ def get_data(data, filter_column_name):
8
+
9
+ data = pd.read_csv(data)
10
+ df = data.head(69)
11
+ #df = df.astype(str)
12
+ df.fillna("No Answer", inplace=True)
13
+
14
+ if filter_column_name is not None:
15
+ if filter_column_name in df.columns:
16
+ filter_by = st.sidebar.selectbox(f"Choose the values from \"{filter_column_name}\" values",df[filter_column_name].unique().tolist())
17
+
18
+ filtered_df = df[df[filter_column_name]==filter_by]
19
+
20
+ return filtered_df, filter_by
21
+
22
+ else:
23
+ st.error(f"No column called \"{filter_column_name}\" in given dataset")
24
+ return None, None
25
+
26
+ else:
27
+ return df, None
28
+
29
+
30
+ def get_view(df, col_list, percent):
31
+
32
+
33
+
34
+ new_df = pd.DataFrame()
35
+
36
+ for col in col_list:
37
+ if col in df.columns:
38
+ if percent==0:
39
+ # Create a Series with the value counts of the current column
40
+ series = df[col].value_counts()
41
+ elif percent==1:
42
+ series = df[col].value_counts(normalize=True)*100
43
+ series = series.round(2)
44
+
45
+
46
+ # Add the Series to the new DataFrame as a new column
47
+ new_df[col] = series
48
+
49
+ else:
50
+ st.error(f"Column: \"{col}\" not found in the dataset to generate view.")
51
+
52
+ view = new_df.T
53
+ view.reset_index(inplace=True)
54
+
55
+ if percent==1:
56
+ st.subheader("Percentage view")
57
+
58
+ st.write(view)