Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -36,4 +36,63 @@ if uploaded_file is not None:
|
|
| 36 |
# Show the categorized data
|
| 37 |
st.write("Categorized Data:", df.head())
|
| 38 |
|
| 39 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
# Show the categorized data
|
| 37 |
st.write("Categorized Data:", df.head())
|
| 38 |
|
| 39 |
+
# Visualization 1: Pie Chart of Spending by Category
|
| 40 |
+
category_expenses = df.groupby('Category')['Amount'].sum()
|
| 41 |
+
|
| 42 |
+
# Plot pie chart for expense distribution by category
|
| 43 |
+
fig1, ax1 = plt.subplots(figsize=(8, 8))
|
| 44 |
+
category_expenses.plot(kind='pie', autopct='%1.1f%%', startangle=90, colors=plt.cm.Paired.colors, ax=ax1)
|
| 45 |
+
ax1.set_title('Expense Distribution by Category')
|
| 46 |
+
ax1.set_ylabel('') # Hide the y-axis label
|
| 47 |
+
st.pyplot(fig1)
|
| 48 |
+
|
| 49 |
+
# Visualization 2: Monthly Spending Trends (Line Chart)
|
| 50 |
+
# Convert 'Date' to datetime
|
| 51 |
+
df['Date'] = pd.to_datetime(df['Date'])
|
| 52 |
+
|
| 53 |
+
# Extract month-year for grouping and convert the Period to string to avoid JSON serialization issues
|
| 54 |
+
df['Month'] = df['Date'].dt.to_period('M').astype(str) # Convert Period to string
|
| 55 |
+
|
| 56 |
+
# Group by month and calculate the total amount spent per month
|
| 57 |
+
monthly_expenses = df.groupby('Month')['Amount'].sum()
|
| 58 |
+
|
| 59 |
+
# Plot monthly spending trends as a line chart
|
| 60 |
+
fig2 = px.line(
|
| 61 |
+
monthly_expenses,
|
| 62 |
+
x=monthly_expenses.index,
|
| 63 |
+
y=monthly_expenses.values,
|
| 64 |
+
title="Monthly Expenses",
|
| 65 |
+
labels={"x": "Month", "y": "Amount ($)"}
|
| 66 |
+
)
|
| 67 |
+
st.plotly_chart(fig2)
|
| 68 |
+
|
| 69 |
+
# Budget and Alerts Example (Tracking if any category exceeds its budget)
|
| 70 |
+
budgets = {
|
| 71 |
+
"Groceries": 300,
|
| 72 |
+
"Rent": 1000,
|
| 73 |
+
"Utilities": 150,
|
| 74 |
+
"Entertainment": 100,
|
| 75 |
+
"Dining": 150,
|
| 76 |
+
"Transportation": 120,
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
# Track if any category exceeds its budget
|
| 80 |
+
df['Budget_Exceeded'] = df.apply(lambda row: row['Amount'] > budgets.get(row['Category'], 0), axis=1)
|
| 81 |
+
|
| 82 |
+
# Show which categories exceeded their budgets
|
| 83 |
+
exceeded_budget = df[df['Budget_Exceeded'] == True]
|
| 84 |
+
st.write("Categories that exceeded the budget:", exceeded_budget[['Date', 'Category', 'Amount']])
|
| 85 |
+
|
| 86 |
+
# Visualization 3: Monthly Spending vs Budget (Bar Chart)
|
| 87 |
+
# Create a figure explicitly for the bar chart
|
| 88 |
+
fig3, ax3 = plt.subplots(figsize=(10, 6)) # Create figure and axes
|
| 89 |
+
monthly_expenses_df = pd.DataFrame({
|
| 90 |
+
'Actual': monthly_expenses,
|
| 91 |
+
'Budget': [sum(budgets.values())] * len(monthly_expenses) # Same budget for simplicity
|
| 92 |
+
})
|
| 93 |
+
monthly_expenses_df.plot(kind='bar', ax=ax3) # Pass the axes to the plot
|
| 94 |
+
ax3.set_title('Monthly Spending vs Budget')
|
| 95 |
+
ax3.set_ylabel('Amount ($)')
|
| 96 |
+
|
| 97 |
+
# Display the plot with Streamlit
|
| 98 |
+
st.pyplot(fig3)
|