File size: 4,715 Bytes
36ef8ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
730fb31
36ef8ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ccd1071
36ef8ab
 
 
 
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
import streamlit as st
import pandas as pd
import math
import matplotlib.pyplot as plt

# Function to read different file types
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

# Function to display the uploaded data
def display_data(data):
    st.write("### Data Preview")
    st.dataframe(data.head())

# Function to perform mathematical calculations and store results as separate columns
def perform_calculations(data):
    st.write("### Calculations")

    # Get column names
    columns = data.columns

    # Iterate over each column and perform calculations
    for column in columns:
        st.write("Calculations for column:", column)

        # Example calculations: sum, mean, median
        column_sum = data[column].sum()
        column_mean = data[column].mean()
        column_median = data[column].median()

        # Create new column names
        sum_column_name = f"{column}_sum"
        mean_column_name = f"{column}_mean"
        median_column_name = f"{column}_median"

        # Add the calculated values as new columns
        data[sum_column_name] = column_sum
        data[mean_column_name] = column_mean
        data[median_column_name] = column_median

        # Display the calculated values
        st.write("Sum:", column_sum)
        st.write("Mean:", column_mean)
        st.write("Median:", column_median)

    # Display the updated data with calculated columns
    st.write("### Updated Data")
    st.dataframe(data)

# Function to perform mathematical calculations
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()