Mithun162001/data-analysis-application
1
1from turtle import color2from pyparsing import col3import streamlit as st4import pandas as pd5import numpy as np6import plotly.express as px7import plotly.graph_objects as go8import base649 10# Function to load data11def load_data(uploaded_file):12 try:13 if uploaded_file.name.endswith('.csv'):14 df = pd.read_csv(uploaded_file)15 elif uploaded_file.name.endswith('.xlsx'):16 df = pd.read_excel(uploaded_file)17 else:18 st.error("Unsupported file type.")19 return None20 return df21 except Exception as e:22 st.error(f"An error occurred: {e}")23 return None24 25# Function to show dataframe26def show_dataframe(df):27 st.write(df)28 29# Function to create surface data30def create_surface_data(df):31 # This function would need to process 'df' to produce 'x', 'y', and 'z' for the surface plot32 # Here we just create a simple example with numpy33 x = np.outer(np.linspace(-10, 10, 30), np.ones(30))34 y = x.copy().T # transpose35 z = np.cos(x ** 2 + y ** 2)36 return x, y, z37 38def get_image_as_base64(path):39 with open(path, "rb") as f:40 return base64.b64encode(f.read()).decode()41 42# Main function where the app runs43def main():44 45 st.set_page_config(page_title="Data Analysis Application", page_icon="๐", layout="wide")46 st.title("Data Analysis Application")47 logo_base64 = get_image_as_base64("hpesm_pri_grn_rev_rgb.png")48 st.sidebar.markdown(49 f'<img src="data:image/png;base64,{logo_base64}" alt="logo" width="200"><br><br>', unsafe_allow_html=True50 )51 # File uploader52 uploaded_file = st.sidebar.file_uploader("Upload your CSV or Excel file.", type=["csv", "xlsx"])53 54 if uploaded_file is not None:55 df = load_data(uploaded_file)56 if df is not None:57 # Home Page Options58 st.sidebar.title("What would you like to do?")59 options = st.sidebar.radio("", ('EDA', 'Data Visualization'), label_visibility="collapsed")60 61 if options == 'EDA':62 # Display EDA options63 eda_option = st.sidebar.selectbox("Choose an EDA option:", 64 ("Show dtypes", "Show columns", "Show summary", "Show missing values", 65 "Show percentage of missing values", "Show number of unique values", 66 "Show skewness and kurtosis", "Check for outliers"), label_visibility="collapsed")67 68 if eda_option == "Show dtypes":69 st.write(df.dtypes)70 elif eda_option == "Show columns":71 st.write(df.columns.tolist())72 elif eda_option == "Show summary":73 st.write(df.describe())74 elif eda_option == "Show missing values":75 st.write(df.isnull().sum())76 elif eda_option == "Show percentage of missing values":77 st.write(df.isnull().mean() * 100)78 elif eda_option == "Show number of unique values":79 st.write(df.nunique())80 elif eda_option == "Show skewness and kurtosis":81 try:82 st.write("Skewness:")83 st.write(df.skew())84 st.write("Kurtosis:")85 st.write(df.kurtosis())86 except Exception as e:87 st.error(f"An error occurred when calculating skewness and kurtosis: {e}")88 elif eda_option == "Check for outliers":89 # Select numeric columns, specify the data types explicitly90 numeric_cols = df.select_dtypes(include=['float64', 'int64']).columns.tolist()91 selected_column = st.sidebar.selectbox("Select Column", numeric_cols, label_visibility="visible")92 if st.button("Show Outliers for Selected Column"):93 fig = px.box(df, y=selected_column)94 st.plotly_chart(fig)95 # Calculate Z-score and display outliers96 z_scores = (df[selected_column] - df[selected_column].mean()) / df[selected_column].std()97 st.write(df[abs(z_scores) > 3])98 99 elif options == 'Data Visualization':100 # Display Data Visualization options101 vis_option = st.sidebar.selectbox("Choose a plot type:", 102 ("Univariate Plots", "Bivariate Plots", "Multivariate Plots"), label_visibility="visible")103 104 selected_color = st.sidebar.color_picker("Pick a color", "#01A982")105 106 # Universal plot settings107 if vis_option == "Univariate Plots":108 column_to_plot = st.sidebar.selectbox("Choose a column to plot:", df.columns, label_visibility="visible")109 plot_type = st.sidebar.selectbox("Choose plot type:", ("Bar", "Box", "Box Plot (enhanced)", "Histogram", 110 "Pie Chart", "Violin Plot", "Density Plot (KDE)", 111 "Area Chart", "Rug Plot", "Cumulative Distribution Function", 112 "Funnel Chart"), label_visibility="visible")113 hue_column = None114 if plot_type in ["Bar", "Box", "Box Plot (enhanced)", "Violin Plot", "Histogram", "Density Plot (KDE)", "Rug Plot", "Cumulative Distribution Function"]:115 hue_options = [None] + list(df.select_dtypes(include=['object']).columns)116 hue_column = st.sidebar.selectbox("Choose a categorical column for color coding hue:", hue_options, format_func=lambda x:'None' if x is None else x, label_visibility="visible")117 if plot_type == "Bar":118 fig = px.bar(df, y=column_to_plot, color=hue_column, color_discrete_sequence=[selected_color] if hue_column is None else None)119 st.plotly_chart(fig)120 elif plot_type == "Box":121 fig = px.box(df, y=column_to_plot, color=hue_column, color_discrete_sequence=[selected_color])122 st.plotly_chart(fig)123 elif plot_type == "Box Plot (enhanced)":124 fig = px.box(df, y=column_to_plot, points="all", color=hue_column, color_discrete_sequence=[selected_color] if hue_column is None else None)125 st.plotly_chart(fig)126 elif plot_type == "Histogram":127 fig = px.histogram(df, x=column_to_plot, color=hue_column, color_discrete_sequence=[selected_color] if hue_column is None else None)128 st.plotly_chart(fig)129 elif plot_type == "Pie Chart":130 fig = px.pie(df, names=column_to_plot, color_discrete_sequence=[selected_color])131 st.plotly_chart(fig)132 elif plot_type == "Violin Plot":133 fig = px.violin(df, y=column_to_plot, color=hue_column, color_discrete_sequence=[selected_color] if hue_column is None else None)134 st.plotly_chart(fig)135 elif plot_type == "Density Plot (KDE)": # KDE = Kernel Density Estimation136 fig = px.density_contour(df, x=column_to_plot, marginal_x="histogram", color=hue_column, color_discrete_sequence=[selected_color] if hue_column is None else None)137 st.plotly_chart(fig)138 elif plot_type == "Area Chart":139 fig = px.area(df, y=column_to_plot, color_discrete_sequence=[selected_color])140 st.plotly_chart(fig)141 elif plot_type == "Rug Plot":142 fig = px.density_contour(df, x=column_to_plot, marginal_x="rug", color=hue_column, color_discrete_sequence=[selected_color] if hue_column is None else None)143 st.plotly_chart(fig)144 elif plot_type == "Cumulative Distribution Function":145 fig = px.histogram(df, x=column_to_plot, cumulative=True, color=hue_column, color_discrete_sequence=[selected_color] if hue_column is None else None)146 st.plotly_chart(fig)147 elif plot_type == "Funnel Chart":148 fig = px.funnel(df, y=column_to_plot, color_discrete_sequence=[selected_color] if hue_column is None else None)149 st.plotly_chart(fig)150 151 elif vis_option == "Bivariate Plots":152 153 # Select plot type154 bivariate_plot_type = st.sidebar.selectbox(155 "Choose bivariate plot type:", 156 ("Scatter Plot", "Line Plot", "Bubble Chart", "Area Chart", 157 "Joint Plot", "Stacked Bar Chart", "Grouped Bar Chart", 158 "Contour Plot", "Box Plot", "Error Bars Plot", "Violin Plot", 159 ), 160 label_visibility="visible"161 )162 # User selects two columns for Bivariate plots163 col1 = st.sidebar.selectbox("Choose the first column:", df.columns, label_visibility="visible")164 col2 = st.sidebar.selectbox("Choose the second column:", df.columns, label_visibility="visible")165 hue_column = None166 if bivariate_plot_type in ["Scatter Plot", "Line Plot", "Bubble Chart", "Area Chart", "Joint Plot", "Stacked Bar Chart", "Grouped Bar Chart", "Contour Plot", "Box Plot", "Error Bars Plot", "Violin Plot"]:167 hue_options = [None] + list(df.select_dtypes(include=['object']).columns)168 hue_column = st.sidebar.selectbox(169 "Choose a categorical column for color coding (hue):", 170 hue_options, 171 format_func=lambda x:'None' if x is None else x, 172 label_visibility="visible"173 )174 175 if bivariate_plot_type == "Scatter Plot":176 fig = px.scatter(df, x=col1, y=col2, color=hue_column, trendline="ols", trendline_color_override="yellow",color_discrete_sequence=[selected_color] if hue_column is None else None)177 st.plotly_chart(fig)178 elif bivariate_plot_type == "Line Plot":179 fig = px.line(df, x=col1, y=col2, color=hue_column, color_discrete_sequence=[selected_color] if hue_column is None else None)180 st.plotly_chart(fig)181 elif bivariate_plot_type == "Bubble Chart":182 size_column = st.sidebar.selectbox("Choose a column for bubble size:", df.columns, label_visibility="visible")183 fig = px.scatter(df, x=col1, y=col2, size=size_column, color=hue_column, color_discrete_sequence=[selected_color] if hue_column is None else None)184 st.plotly_chart(fig)185 elif bivariate_plot_type == "Area Chart":186 fig = px.area(df, x=col1, y=col2, color=hue_column, color_discrete_sequence=[selected_color] if hue_column is None else None)187 st.plotly_chart(fig)188 elif bivariate_plot_type == "Joint Plot":189 fig = px.scatter(df, x=col1, y=col2, color=hue_column, marginal_x="histogram", marginal_y="histogram", trendline="ols", trendline_color_override="yellow",color_discrete_sequence=[selected_color] if hue_column is None else None)190 st.plotly_chart(fig)191 elif bivariate_plot_type == "Stacked Bar Chart":192 fig = px.bar(df, x=col1, y=col2, color=hue_column, color_discrete_sequence=[selected_color] if hue_column is None else None)193 st.plotly_chart(fig)194 elif bivariate_plot_type == "Grouped Bar Chart":195 fig = px.bar(df, x=col1, y=col2, color=hue_column, barmode="group", color_discrete_sequence=[selected_color] if hue_column is None else None)196 st.plotly_chart(fig)197 elif bivariate_plot_type == "Contour Plot":198 fig = px.density_contour(df, x=col1, y=col2, color=hue_column, color_discrete_sequence=[selected_color] if hue_column is None else None)199 st.plotly_chart(fig)200 elif bivariate_plot_type == "Box Plot":201 fig = px.box(df, x=col1, y=col2, color=hue_column, color_discrete_sequence=[selected_color] if hue_column is None else None)202 st.plotly_chart(fig)203 elif bivariate_plot_type == "Error Bars Plot":204 fig = px.scatter(df, x=col1, y=col2, color=hue_column, error_x=col1, error_y=col2, color_discrete_sequence=[selected_color] if hue_column is None else None)205 st.plotly_chart(fig)206 elif bivariate_plot_type == "Violin Plot":207 fig = px.violin(df, x=col1, y=col2, color=hue_column, color_discrete_sequence=[selected_color] if hue_column is None else None)208 st.plotly_chart(fig)209 210 211 elif vis_option == "Multivariate Plots":212 # Ensure `selected_columns` is a list, not a Pandas Index or Series.213 selected_columns = st.sidebar.multiselect("Choose columns for multivariate plot:",214 options=df.columns.tolist(),215 default=df.columns[:3].tolist(),216 label_visibility="visible")217 218 # Check if the selection is not empty.219 if selected_columns: # This checks if the list is not empty220 # Now we can proceed to select the plot type.221 multivariate_plot_type = st.sidebar.selectbox(222 "Choose multivariate plot type:",223 ("3D Scatter Plot", "Parallel Coordinates", "Ternary Plot", "3D Surface Plot"),224 label_visibility="visible"225 )226 227 if multivariate_plot_type == "3D Scatter Plot":228 # Ensure that three distinct columns have been chosen.229 if len(selected_columns) >= 3:230 col1, col2, col3 = selected_columns[:3] # Take the first three selections231 fig = px.scatter_3d(df, x=col1, y=col2, z=col3, color=col1)232 st.plotly_chart(fig)233 else:234 st.error("Please select at least three columns for the 3D Scatter Plot.")235 236 elif multivariate_plot_type == "Parallel Coordinates":237 fig = px.parallel_coordinates(df, color=selected_columns[0])238 st.plotly_chart(fig)239 240 elif multivariate_plot_type == "Ternary Plot":241 # Ensure that three distinct columns have been chosen.242 if len(selected_columns) >= 3:243 col1, col2, col3 = selected_columns[:3]244 fig = px.scatter_ternary(df, a=col1, b=col2, c=col3, color=col1)245 st.plotly_chart(fig)246 else:247 st.error("Please select at least three columns for the Ternary Plot.")248 249 elif multivariate_plot_type == "3D Surface Plot":250 # Ensure that three distinct columns have been chosen.251 if len(selected_columns) >= 3:252 col1, col2, col3 = selected_columns[:3]253 x, y, z = create_surface_data(df)254 fig = go.Figure(data=[go.Surface(x=x, y=y, z=z)])255 st.plotly_chart(fig)256 else:257 st.error("Please select at least three columns for the 3D Surface Plot.")258 259 else:260 st.warning("Please select at least one column to create a plot.")261 262 263 264if __name__ == "__main__":265 main()