Qasim0777 commited on
Commit
282b6be
·
verified ·
1 Parent(s): d620ff2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +126 -102
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("HBL Transactions Dashboard")
12
- st.sidebar.header("Navigation")
13
- options = st.sidebar.selectbox("Select Visualization", [
14
- "Account Type Distribution",
15
- "Transaction Flow by Beneficiary Bank",
16
- "Geographic Heatmap of Transactions",
17
- "Anomalies in Transactions",
18
- "Comparative Analysis of Transaction Types",
19
- "Time-Based Analysis",
20
- "Customer Insights"
21
- ])
22
-
23
- # Task 1: Account Type Distribution
24
- if options == "Account Type Distribution":
25
- st.header("Account Type Distribution")
26
- account_type_distribution = data['Account Type'].value_counts().reset_index()
27
- account_type_distribution.columns = ['Account Type', 'Count']
28
-
29
- fig, ax = plt.subplots(figsize=(8, 6))
30
- sns.barplot(data=account_type_distribution, x='Account Type', y='Count', palette='coolwarm', ax=ax)
31
- ax.set_title('Account Type Distribution')
32
- st.pyplot(fig)
33
-
34
- # Task 2: Transaction Flow by Beneficiary Bank
35
- elif options == "Transaction Flow by Beneficiary Bank":
36
- st.header("Transaction Flow by Beneficiary Bank")
37
- top_beneficiary_banks = data.groupby(['Region', 'Transaction To'])['Credit'].sum().reset_index()
38
- top_beneficiary_banks = top_beneficiary_banks.sort_values(by='Credit', ascending=False).groupby('Region').head(5)
39
-
40
- fig, ax = plt.subplots(figsize=(12, 8))
41
- sns.barplot(data=top_beneficiary_banks, x='Credit', y='Transaction To', hue='Region', palette='viridis', ax=ax)
42
- ax.set_title('Top 5 Beneficiary Banks by Region')
43
- st.pyplot(fig)
44
-
45
- # Task 3: Geographic Heatmap of Transactions
46
- elif options == "Geographic Heatmap of Transactions":
47
- st.header("Geographic Heatmap of Transactions")
48
- heatmap_data = data.groupby('Region')[['Credit', 'Debit']].sum().reset_index()
49
- heatmap_matrix = heatmap_data.set_index('Region')
50
-
51
- fig, ax = plt.subplots(figsize=(10, 6))
52
- sns.heatmap(heatmap_matrix, annot=True, fmt='.0f', cmap='YlGnBu', cbar=True, ax=ax)
53
- ax.set_title('Geographic Heatmap of Transactions')
54
- st.pyplot(fig)
55
-
56
- # Task 4: Anomalies in Transactions
57
- elif options == "Anomalies in Transactions":
58
- st.header("Anomalies in Transactions")
59
- from scipy.stats import zscore
60
-
61
- data['Credit_Z'] = zscore(data['Credit'])
62
- data['Debit_Z'] = zscore(data['Debit'])
63
-
64
- credit_anomalies = data[data['Credit_Z'].abs() > 3]
65
- debit_anomalies = data[data['Debit_Z'].abs() > 3]
66
-
67
- fig, ax = plt.subplots(figsize=(12, 6))
68
- sns.scatterplot(x=data.index, y=data['Credit'], label='Credit Transactions', alpha=0.6, color='blue', ax=ax)
69
- sns.scatterplot(x=credit_anomalies.index, y=credit_anomalies['Credit'], label='Credit Anomalies', color='red', ax=ax)
70
- sns.scatterplot(x=data.index, y=data['Debit'], label='Debit Transactions', alpha=0.6, color='green', ax=ax)
71
- sns.scatterplot(x=debit_anomalies.index, y=debit_anomalies['Debit'], label='Debit Anomalies', color='orange', ax=ax)
72
- ax.set_title('Anomalies in Transactions')
73
- ax.legend()
74
- st.pyplot(fig)
75
-
76
- # Task 5: Comparative Analysis of Transaction Types
77
- elif options == "Comparative Analysis of Transaction Types":
78
- st.header("Comparative Analysis of Transaction Types")
79
- melted_data = data.melt(id_vars=['Account Type'], value_vars=['Credit', 'Debit'],
80
- var_name='Transaction Type', value_name='Amount')
81
-
82
- fig, ax = plt.subplots(figsize=(12, 8))
83
- sns.boxplot(data=melted_data, x='Account Type', y='Amount', hue='Transaction Type', palette='Set2', ax=ax)
84
- ax.set_title('Comparative Analysis of Transaction Types')
85
- st.pyplot(fig)
86
-
87
- # Task 6: Time-Based Analysis
88
- elif options == "Time-Based Analysis":
89
- st.header("Time-Based Analysis")
90
- if 'Time' in data.columns:
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 7: Customer Insights
102
- elif options == "Customer Insights":
103
- st.header("Customer Insights")
104
- if 'Customer Type Description' in data.columns:
105
- customer_data = data.groupby('Customer Type Description')[['Credit', 'Debit']].sum().reset_index()
106
- customer_data.set_index('Customer Type Description', inplace=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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