forked from chetannihith/python-hacktoberfest25
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPERSONAL_FINANCE_DASHBOARD
More file actions
92 lines (75 loc) · 2.84 KB
/
PERSONAL_FINANCE_DASHBOARD
File metadata and controls
92 lines (75 loc) · 2.84 KB
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
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from datetime import datetime
import os
# File to store transactions
FILE_NAME = "finance_data.csv"
# Check if file exists; if not, create it
if not os.path.exists(FILE_NAME):
df = pd.DataFrame(columns=["Date", "Category", "Amount", "Type"])
df.to_csv(FILE_NAME, index=False)
# Function to add transaction
def add_transaction():
date = input("Enter date (YYYY-MM-DD): ")
category = input("Enter category (Food, Rent, Entertainment, etc.): ")
amount = float(input("Enter amount: "))
t_type = input("Enter type (Income/Expense): ")
df = pd.read_csv(FILE_NAME)
df = df.append({"Date": date, "Category": category, "Amount": amount, "Type": t_type}, ignore_index=True)
df.to_csv(FILE_NAME, index=False)
print("Transaction added successfully!")
# Function to show dashboard
def show_dashboard():
df = pd.read_csv(FILE_NAME)
df['Date'] = pd.to_datetime(df['Date'])
# Monthly summary
df['Month'] = df['Date'].dt.to_period('M')
monthly_expense = df[df['Type'] == 'Expense'].groupby('Month')['Amount'].sum()
monthly_income = df[df['Type'] == 'Income'].groupby('Month')['Amount'].sum()
print("\nMonthly Expense:")
print(monthly_expense)
print("\nMonthly Income:")
print(monthly_income)
# Visualization
monthly_expense.plot(kind='bar', color='red', title='Monthly Expenses')
plt.ylabel("Amount")
plt.show()
monthly_income.plot(kind='bar', color='green', title='Monthly Income')
plt.ylabel("Amount")
plt.show()
# Function to forecast next month expense
def forecast_expense():
df = pd.read_csv(FILE_NAME)
df['Date'] = pd.to_datetime(df['Date'])
df['MonthNum'] = df['Date'].dt.month + (df['Date'].dt.year * 12)
monthly_expense = df[df['Type'] == 'Expense'].groupby('MonthNum')['Amount'].sum().reset_index()
X = monthly_expense['MonthNum'].values.reshape(-1,1)
y = monthly_expense['Amount'].values
model = LinearRegression()
model.fit(X, y)
next_month = [[X[-1][0] + 1]]
prediction = model.predict(next_month)
print(f"Forecasted expense for next month: ₹{prediction[0]:.2f}")
# Main Menu
def main():
while True:
print("\n--- Personal Finance Dashboard ---")
print("1. Add Transaction")
print("2. Show Dashboard")
print("3. Forecast Next Month Expense")
print("4. Exit")
choice = input("Enter choice: ")
if choice == '1':
add_transaction()
elif choice == '2':
show_dashboard()
elif choice == '3':
forecast_expense()
elif choice == '4':
print("Exiting...")
break
else:
print("Invalid choice! Try again.")
if __name__ == "__main__":
main()