| import streamlit as st |
| import pandas as pd |
| import math |
| import matplotlib.pyplot as plt |
|
|
| |
| def read_file(file): |
| file_extension = file.name.split(".")[-1] |
| if file_extension == "csv": |
| data = pd.read_csv(file) |
| elif file_extension == "xlsx": |
| data = pd.read_excel(file, engine="openpyxl") |
| else: |
| st.error("Unsupported file format. Please upload a CSV or Excel file.") |
| return None |
| return data |
|
|
| |
| def display_data(data): |
| st.write("### Data Preview") |
| st.dataframe(data.head()) |
|
|
| |
| def perform_calculations(data): |
| st.write("### Calculations") |
|
|
| |
| columns = data.columns |
|
|
| |
| for column in columns: |
| st.write("Calculations for column:", column) |
|
|
| |
| column_sum = data[column].sum() |
| column_mean = data[column].mean() |
| column_median = data[column].median() |
|
|
| |
| sum_column_name = f"{column}_sum" |
| mean_column_name = f"{column}_mean" |
| median_column_name = f"{column}_median" |
|
|
| |
| data[sum_column_name] = column_sum |
| data[mean_column_name] = column_mean |
| data[median_column_name] = column_median |
|
|
| |
| st.write("Sum:", column_sum) |
| st.write("Mean:", column_mean) |
| st.write("Median:", column_median) |
|
|
| |
| st.write("### Updated Data") |
| st.dataframe(data) |
|
|
| |
| def perform_math(df, selected_columns, operation): |
| result = None |
|
|
| if operation == "sqrt": |
| result = df[selected_columns].applymap(math.sqrt) |
| elif operation == "log": |
| result = df[selected_columns].applymap(math.log) |
| elif operation == "exp": |
| result = df[selected_columns].applymap(math.exp) |
| elif operation == "sin": |
| result = df[selected_columns].applymap(math.sin) |
| elif operation == "cos": |
| result = df[selected_columns].applymap(math.cos) |
| elif operation == "tan": |
| result = df[selected_columns].applymap(math.tan) |
| elif operation == "multiply": |
| result = df[selected_columns].prod(axis=1) |
| elif operation == "add": |
| result = df[selected_columns].sum(axis=1) |
| elif operation == "subtract": |
| result = df[selected_columns[0]] - df[selected_columns[1]] |
|
|
| if result is not None: |
| df[f"{operation}_result"] = result |
|
|
| return df |
|
|
| def plot_graph(data, graph_type, x_variables, y_variables): |
| plt.figure(figsize=(8, 6)) |
|
|
| for x_var in x_variables: |
| for y_var in y_variables: |
| if graph_type == "Scatter": |
| plt.scatter(data[x_var], data[y_var], label=f"{x_var} vs {y_var}") |
| elif graph_type == "Line": |
| plt.plot(data[x_var], data[y_var], label=f"{x_var} vs {y_var}") |
| st.pyplot() |
| elif graph_type == "Bar": |
| x = range(len(data)) |
| plt.bar(x, data[y_var], label=y_var) |
|
|
| plt.xlabel("X Values") |
| plt.ylabel("Y Values") |
| plt.title(f"{graph_type} Plot") |
| plt.legend() |
|
|
| st.pyplot() |
|
|
| def main(): |
| st.title("Excel-like Data Visualization and Calculations") |
| st.write("Upload a CSV or Excel file and visualize the data") |
|
|
| file = st.file_uploader("Upload file", type=["csv", "xlsx"]) |
|
|
| if file is not None: |
| data = read_file(file) |
| if data is not None: |
| display_data(data) |
| perform_calculations(data) |
|
|
| st.write("### Graph Visualizer") |
| st.write("Select variables for visualization:") |
|
|
| graph_type = st.selectbox("Graph Type", options=["Scatter", "Line", "Bar"]) |
| x_variables = st.multiselect("X Variables", options=data.columns) |
| y_variables = st.multiselect("Y Variables", options=data.columns) |
|
|
| selected_columns = st.multiselect("Select columns:", options=data.columns) |
| operation = st.selectbox("Select an operation:", ["sqrt", "log", "exp", "sin", "cos", "tan", "multiply", "add", "subtract"]) |
|
|
| if st.button("Calculate"): |
| data = perform_math(data, selected_columns, operation) |
| st.write(data) |
| if st.button("Plot"): |
| plot_graph(data, graph_type, x_variables, y_variables) |
| |
|
|
| if __name__ == "__main__": |
| st.set_page_config(page_title="My Analytics App") |
| main() |
|
|