ec7719 commited on
Commit
36ef8ab
·
1 Parent(s): 72ddcea

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +136 -0
app.py ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import math
4
+ import matplotlib.pyplot as plt
5
+
6
+ # Function to read different file types
7
+ def read_file(file):
8
+ file_extension = file.name.split(".")[-1]
9
+ if file_extension == "csv":
10
+ data = pd.read_csv(file)
11
+ elif file_extension == "xlsx":
12
+ data = pd.read_excel(file, engine="openpyxl")
13
+ else:
14
+ st.error("Unsupported file format. Please upload a CSV or Excel file.")
15
+ return None
16
+ return data
17
+
18
+ # Function to display the uploaded data
19
+ def display_data(data):
20
+ st.write("### Data Preview")
21
+ st.dataframe(data.head())
22
+
23
+ # Function to perform mathematical calculations and store results as separate columns
24
+ def perform_calculations(data):
25
+ st.write("### Calculations")
26
+
27
+ # Get column names
28
+ columns = data.columns
29
+
30
+ # Iterate over each column and perform calculations
31
+ for column in columns:
32
+ st.write("Calculations for column:", column)
33
+
34
+ # Example calculations: sum, mean, median
35
+ column_sum = data[column].sum()
36
+ column_mean = data[column].mean()
37
+ column_median = data[column].median()
38
+
39
+ # Create new column names
40
+ sum_column_name = f"{column}_sum"
41
+ mean_column_name = f"{column}_mean"
42
+ median_column_name = f"{column}_median"
43
+
44
+ # Add the calculated values as new columns
45
+ data[sum_column_name] = column_sum
46
+ data[mean_column_name] = column_mean
47
+ data[median_column_name] = column_median
48
+
49
+ # Display the calculated values
50
+ st.write("Sum:", column_sum)
51
+ st.write("Mean:", column_mean)
52
+ st.write("Median:", column_median)
53
+
54
+ # Display the updated data with calculated columns
55
+ st.write("### Updated Data")
56
+ st.dataframe(data)
57
+
58
+ # Function to perform mathematical calculations
59
+ def perform_math(df, selected_columns, operation):
60
+ result = None
61
+
62
+ if operation == "sqrt":
63
+ result = df[selected_columns].applymap(math.sqrt)
64
+ elif operation == "log":
65
+ result = df[selected_columns].applymap(math.log)
66
+ elif operation == "exp":
67
+ result = df[selected_columns].applymap(math.exp)
68
+ elif operation == "sin":
69
+ result = df[selected_columns].applymap(math.sin)
70
+ elif operation == "cos":
71
+ result = df[selected_columns].applymap(math.cos)
72
+ elif operation == "tan":
73
+ result = df[selected_columns].applymap(math.tan)
74
+ elif operation == "multiply":
75
+ result = df[selected_columns].prod(axis=1)
76
+ elif operation == "add":
77
+ result = df[selected_columns].sum(axis=1)
78
+ elif operation == "subtract":
79
+ result = df[selected_columns[0]] - df[selected_columns[1]]
80
+
81
+ if result is not None:
82
+ df[f"{operation}_result"] = result
83
+
84
+ return df
85
+
86
+ def plot_graph(data, graph_type, x_variables, y_variables):
87
+ plt.figure(figsize=(8, 6))
88
+
89
+ for x_var in x_variables:
90
+ for y_var in y_variables:
91
+ if graph_type == "Scatter":
92
+ plt.scatter(data[x_var], data[y_var], label=f"{x_var} vs {y_var}")
93
+ elif graph_type == "Line":
94
+ plt.plot(data[x_var], data[y_var], label=f"{x_var} vs {y_var}")
95
+ elif graph_type == "Bar":
96
+ x = range(len(data))
97
+ plt.bar(x, data[y_var], label=y_var)
98
+
99
+ plt.xlabel("X Values")
100
+ plt.ylabel("Y Values")
101
+ plt.title(f"{graph_type} Plot")
102
+ plt.legend()
103
+
104
+ st.pyplot()
105
+
106
+ def main():
107
+ st.title("Excel-like Data Visualization and Calculations")
108
+ st.write("Upload a CSV or Excel file and visualize the data")
109
+
110
+ file = st.file_uploader("Upload file", type=["csv", "xlsx"])
111
+
112
+ if file is not None:
113
+ data = read_file(file)
114
+ if data is not None:
115
+ display_data(data)
116
+ perform_calculations(data)
117
+
118
+ st.write("### Graph Visualizer")
119
+ st.write("Select variables for visualization:")
120
+
121
+ graph_type = st.selectbox("Graph Type", options=["Scatter", "Line", "Bar"])
122
+ x_variables = st.multiselect("X Variables", options=data.columns)
123
+ y_variables = st.multiselect("Y Variables", options=data.columns)
124
+
125
+ selected_columns = st.multiselect("Select columns:", options=data.columns)
126
+ operation = st.selectbox("Select an operation:", ["sqrt", "log", "exp", "sin", "cos", "tan", "multiply", "add", "subtract"])
127
+
128
+ if st.button("Calculate"):
129
+ data = perform_math(data, selected_columns, operation)
130
+ st.write(data)
131
+ if st.button("Plot"):
132
+ plot_graph(data, graph_type, x_variables, y_variables)
133
+
134
+ if __name__ == "__main__":
135
+ st.set_page_config(page_title="My Analytics App")
136
+ main()