Introduction
Welcome
to PythonSage! In this blog post, we'll guide you step-by-step to create a
simple personal finance tracker using Python and Pandas. This project will help
you manage your finances by tracking your income and expenses, categorizing
transactions, and visualizing your spending habits. By the end, you'll have a
functional personal finance tracker that you can customize and expand.
Getting
Started
Before we dive into the code, make sure you have Python and Pandas installed. You can install Pandas using pip:
pip install pandas
Step 1: Setting Up the Data
First, we create a CSV file named “transactions.csv” to store our financial transactions. This file contains columns for the date, description, category (either 'Income' or 'Expense'), and amount of each transaction.
Date,Description,Category,Amount
2024-07-01,Salary,Income,5000
2024-07-02,Rent,Expense,1500
2024-07-03,Groceries,Expense,200
2024-07-04,Utilities,Expense,100
2024-07-05,Entertainment,Expense,50
Step
2: Loading the Data
We load the data from the CSV file into a Pandas DataFrame. Pandas is a powerful library for data manipulation and analysis in Python.
import pandas as pd
# Load the data
df = pd.read_csv('transactions.csv')
# Display the data
print(df)
Here,
pd.read_csv('transactions.csv') reads the CSV file and stores the data in the
DataFrame df. print(df) displays the contents of the DataFrame.
Step
3: Analyzing the Data
Next, we calculate the total income, total expenses, and the balance.
# Calculate total income and expenses
total_income = df[df['Category'] == 'Income']['Amount'].sum()
total_expenses = df[df['Category'] == 'Expense']['Amount'].sum()
# Calculate balance
balance = total_income - total_expenses
# Display the results
print(f"Total Income: SAR{total_income}")
print(f"Total Expenses: SAR{total_expenses}")
print(f"Balance: SAR{balance}")
Here, df[df['Category'] == 'Income']['Amount'].sum() filters the DataFrame to include only rows where the category is 'Income' and then sums the amounts. The same is done for expenses. The balance is calculated by subtracting total expenses from total income. Finally, we print the results.
Step
4: Visualizing the Data
We use Matplotlib to create a bar chart that shows expenses by category.
import matplotlib.pyplot as plt
# 1. Filter for expenses
expenses = df[df['Category'] == 'Expense']
# 2. Group expenses by description
grouped_expenses = expenses.groupby('Description')
# 3. Sum the amounts for each group
expenses_by_category = grouped_expenses['Amount'].sum()
# Plot the data
expenses_by_category.plot(kind='bar')
plt.title('Expenses by Category')
plt.xlabel('Category')
plt.ylabel('Amount')
plt.show()
Here, df[df['Category'] == 'Expense'].groupby('Description')['Amount'].sum() groups the expense transactions by their description and sums the amounts. expenses_by_category.plot(kind='bar') creates a bar chart, and plt.show() displays it.
Full Code of Personal Finance Tracker
Here is the complete code for our personal finance tracker:
import pandas as pd
import matplotlib.pyplot as plt
# Load the data
df = pd.read_csv('transactions.csv')
# Display the data
print(df)
# Calculate total income and expenses
total_income = df[df['Category'] == 'Income']['Amount'].sum()
total_expenses = df[df['Category'] == 'Expense']['Amount'].sum()
# Calculate balance
balance = total_income - total_expenses
# Display the results
print(f"Total Income: ${total_income}")
print(f"Total Expenses: ${total_expenses}")
print(f"Balance: ${balance}")
# Step-by-step simplification
# 1. Filter for expenses
expenses = df[df['Category'] == 'Expense']
# 2. Group expenses by description
grouped_expenses = expenses.groupby('Description')
# 3. Sum the amounts for each group
expenses_by_category = grouped_expenses['Amount'].sum()
# Plot the data
expenses_by_category.plot(kind='bar')
plt.title('Expenses by Category')
plt.xlabel('Category')
plt.ylabel('Amount')
plt.show()
Conclusion
Congratulations!
You've created a simple personal finance tracker using Python and Pandas. This
project is a great starting point for managing your finances and can be
customized to fit your needs. Experiment with different features and
visualizations to make it even more powerful.
External Links to Learn More
Pandas
Documentation
By
following this guide, you'll gain valuable skills in data analysis and
visualization, which are crucial for managing personal finances effectively.
Happy coding from PythonSage!
How can i contact you and reaches to work together.
riyanabdullah980@gmail.com