Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,110 +2,134 @@ import streamlit as st
|
|
| 2 |
import pandas as pd
|
| 3 |
import seaborn as sns
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
-
|
| 6 |
-
# Load the dataset
|
| 7 |
-
file_path = 'Enhanced_Dummy_HBL_Data.xlsx' # Update this path as needed
|
| 8 |
-
data = pd.read_excel(file_path)
|
| 9 |
|
| 10 |
# Set up the Streamlit page
|
| 11 |
-
st.title("
|
| 12 |
-
st.sidebar.header("Navigation")
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
"
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
st.
|
| 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 |
-
data['Time'] = pd.to_datetime(data['Time'])
|
| 92 |
-
time_data = data.groupby(data['Time'].dt.to_period('M'))[['Credit', 'Debit']].sum().reset_index()
|
| 93 |
-
time_data['Time'] = time_data['Time'].dt.to_timestamp()
|
| 94 |
-
|
| 95 |
-
fig, ax = plt.subplots(figsize=(12, 8))
|
| 96 |
-
sns.lineplot(data=time_data, x='Time', y='Credit', label='Credit', marker='o', ax=ax)
|
| 97 |
-
sns.lineplot(data=time_data, x='Time', y='Debit', label='Debit', marker='o', ax=ax)
|
| 98 |
-
ax.set_title('Transaction Trends Over Time')
|
| 99 |
st.pyplot(fig)
|
| 100 |
|
| 101 |
-
# Task
|
| 102 |
-
elif options == "
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
-
fig, ax = plt.subplots(figsize=(10, 8))
|
| 109 |
-
customer_data.plot(kind='bar', stacked=True, colormap='coolwarm', ax=ax)
|
| 110 |
-
ax.set_title('Total Credit and Debit by Customer Type')
|
| 111 |
-
st.pyplot(fig)
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import seaborn as sns
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
+
from scipy.stats import zscore
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# Set up the Streamlit page
|
| 8 |
+
st.title("Data Visualization")
|
| 9 |
+
st.sidebar.header("Upload and Navigation")
|
| 10 |
+
|
| 11 |
+
# File upload
|
| 12 |
+
uploaded_file = st.sidebar.file_uploader("Upload your Excel file", type=["xlsx"])
|
| 13 |
+
|
| 14 |
+
if uploaded_file:
|
| 15 |
+
# Load the dataset
|
| 16 |
+
data = pd.read_excel(uploaded_file)
|
| 17 |
+
st.sidebar.success("File uploaded successfully!")
|
| 18 |
+
st.write("### Dataset Overview")
|
| 19 |
+
st.dataframe(data.head())
|
| 20 |
+
|
| 21 |
+
# Navigation options
|
| 22 |
+
options = st.sidebar.selectbox("Select Visualization", [
|
| 23 |
+
"Distribution of a Column",
|
| 24 |
+
"Top-N Categories by Aggregated Value",
|
| 25 |
+
"Heatmap of Numerical Data",
|
| 26 |
+
"Outlier Detection",
|
| 27 |
+
"Box Plot Comparison",
|
| 28 |
+
"Time Series Analysis",
|
| 29 |
+
"Stacked Bar Chart"
|
| 30 |
+
])
|
| 31 |
+
|
| 32 |
+
# Task 1: Distribution of a Column
|
| 33 |
+
if options == "Distribution of a Column":
|
| 34 |
+
st.header("Distribution of a Column")
|
| 35 |
+
column = st.selectbox("Select Column for Distribution", data.columns)
|
| 36 |
+
if data[column].dtype in ['int64', 'float64', 'object']:
|
| 37 |
+
fig, ax = plt.subplots(figsize=(8, 6))
|
| 38 |
+
if data[column].dtype == 'object':
|
| 39 |
+
sns.countplot(data=data, x=column, palette='coolwarm', ax=ax)
|
| 40 |
+
else:
|
| 41 |
+
sns.histplot(data=data, x=column, kde=True, color='blue', ax=ax)
|
| 42 |
+
ax.set_title(f'Distribution of {column}')
|
| 43 |
+
st.pyplot(fig)
|
| 44 |
+
else:
|
| 45 |
+
st.error("Selected column is not suitable for distribution visualization.")
|
| 46 |
+
|
| 47 |
+
# Task 2: Top-N Categories by Aggregated Value
|
| 48 |
+
elif options == "Top-N Categories by Aggregated Value":
|
| 49 |
+
st.header("Top-N Categories by Aggregated Value")
|
| 50 |
+
category_column = st.selectbox("Select Category Column", data.columns)
|
| 51 |
+
numeric_column = st.selectbox("Select Numeric Column", data.columns)
|
| 52 |
+
n = st.slider("Select Top-N Categories", 1, 20, 5)
|
| 53 |
+
if data[category_column].dtype == 'object' and data[numeric_column].dtype in ['int64', 'float64']:
|
| 54 |
+
grouped_data = data.groupby(category_column)[numeric_column].sum().reset_index()
|
| 55 |
+
top_n = grouped_data.sort_values(by=numeric_column, ascending=False).head(n)
|
| 56 |
+
fig, ax = plt.subplots(figsize=(10, 6))
|
| 57 |
+
sns.barplot(data=top_n, x=numeric_column, y=category_column, palette='viridis', ax=ax)
|
| 58 |
+
ax.set_title(f'Top-{n} {category_column} by {numeric_column}')
|
| 59 |
+
st.pyplot(fig)
|
| 60 |
+
else:
|
| 61 |
+
st.error("Ensure you select a categorical column and a numeric column.")
|
| 62 |
+
|
| 63 |
+
# Task 3: Heatmap of Numerical Data
|
| 64 |
+
elif options == "Heatmap of Numerical Data":
|
| 65 |
+
st.header("Heatmap of Numerical Data")
|
| 66 |
+
numeric_columns = data.select_dtypes(include=['int64', 'float64']).columns
|
| 67 |
+
if len(numeric_columns) > 1:
|
| 68 |
+
heatmap_data = data[numeric_columns].corr()
|
| 69 |
+
fig, ax = plt.subplots(figsize=(10, 6))
|
| 70 |
+
sns.heatmap(heatmap_data, annot=True, cmap='YlGnBu', ax=ax)
|
| 71 |
+
ax.set_title("Correlation Heatmap")
|
| 72 |
+
st.pyplot(fig)
|
| 73 |
+
else:
|
| 74 |
+
st.error("Dataset does not have enough numerical columns for a heatmap.")
|
| 75 |
+
|
| 76 |
+
# Task 4: Outlier Detection
|
| 77 |
+
elif options == "Outlier Detection":
|
| 78 |
+
st.header("Outlier Detection")
|
| 79 |
+
numeric_column = st.selectbox("Select Numeric Column for Outlier Detection", data.select_dtypes(include=['int64', 'float64']).columns)
|
| 80 |
+
threshold = st.slider("Select Z-Score Threshold", 1.0, 5.0, 3.0)
|
| 81 |
+
data['Z_Score'] = zscore(data[numeric_column])
|
| 82 |
+
outliers = data[data['Z_Score'].abs() > threshold]
|
| 83 |
+
fig, ax = plt.subplots(figsize=(10, 6))
|
| 84 |
+
sns.scatterplot(data=data, x=data.index, y=numeric_column, label='Data', color='blue', ax=ax)
|
| 85 |
+
sns.scatterplot(data=outliers, x=outliers.index, y=numeric_column, label='Outliers', color='red', ax=ax)
|
| 86 |
+
ax.set_title(f'Outlier Detection in {numeric_column}')
|
| 87 |
+
ax.legend()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
st.pyplot(fig)
|
| 89 |
|
| 90 |
+
# Task 5: Box Plot Comparison
|
| 91 |
+
elif options == "Box Plot Comparison":
|
| 92 |
+
st.header("Box Plot Comparison")
|
| 93 |
+
category_column = st.selectbox("Select Categorical Column", data.columns)
|
| 94 |
+
numeric_column = st.selectbox("Select Numeric Column", data.columns)
|
| 95 |
+
if data[category_column].dtype == 'object' and data[numeric_column].dtype in ['int64', 'float64']:
|
| 96 |
+
fig, ax = plt.subplots(figsize=(12, 8))
|
| 97 |
+
sns.boxplot(data=data, x=category_column, y=numeric_column, palette='Set2', ax=ax)
|
| 98 |
+
ax.set_title(f'Box Plot of {numeric_column} by {category_column}')
|
| 99 |
+
st.pyplot(fig)
|
| 100 |
+
else:
|
| 101 |
+
st.error("Ensure you select a categorical column and a numeric column.")
|
| 102 |
+
|
| 103 |
+
# Task 6: Time Series Analysis
|
| 104 |
+
elif options == "Time Series Analysis":
|
| 105 |
+
st.header("Time Series Analysis")
|
| 106 |
+
if 'Time' in data.columns:
|
| 107 |
+
data['Time'] = pd.to_datetime(data['Time'])
|
| 108 |
+
time_column = st.selectbox("Select Time Column", ['Time'])
|
| 109 |
+
value_column = st.selectbox("Select Value Column", data.select_dtypes(include=['int64', 'float64']).columns)
|
| 110 |
+
time_data = data.groupby(data[time_column].dt.to_period('M'))[value_column].sum().reset_index()
|
| 111 |
+
time_data[time_column] = time_data[time_column].dt.to_timestamp()
|
| 112 |
+
fig, ax = plt.subplots(figsize=(12, 8))
|
| 113 |
+
sns.lineplot(data=time_data, x=time_column, y=value_column, ax=ax, marker='o')
|
| 114 |
+
ax.set_title(f'Time Series Analysis of {value_column}')
|
| 115 |
+
st.pyplot(fig)
|
| 116 |
+
else:
|
| 117 |
+
st.error("The dataset does not have a time column.")
|
| 118 |
+
|
| 119 |
+
# Task 7: Stacked Bar Chart
|
| 120 |
+
elif options == "Stacked Bar Chart":
|
| 121 |
+
st.header("Stacked Bar Chart")
|
| 122 |
+
category_column = st.selectbox("Select Categorical Column", data.columns)
|
| 123 |
+
numeric_columns = st.multiselect("Select Numeric Columns", data.select_dtypes(include=['int64', 'float64']).columns)
|
| 124 |
+
if len(numeric_columns) > 1 and category_column:
|
| 125 |
+
grouped_data = data.groupby(category_column)[numeric_columns].sum()
|
| 126 |
+
fig, ax = plt.subplots(figsize=(12, 8))
|
| 127 |
+
grouped_data.plot(kind='bar', stacked=True, ax=ax, colormap='coolwarm')
|
| 128 |
+
ax.set_title(f'Stacked Bar Chart of {", ".join(numeric_columns)} by {category_column}')
|
| 129 |
+
st.pyplot(fig)
|
| 130 |
+
else:
|
| 131 |
+
st.error("Ensure you select one categorical column and multiple numeric columns.")
|
| 132 |
+
|
| 133 |
+
else:
|
| 134 |
+
st.warning("Please upload an Excel file to begin.")
|
| 135 |
|
|
|
|
|
|
|
|
|
|
|
|