Create the Expense DataFrame
Intermediate · Write the code · from Build a Budget Tracker and Expense Analyzer
From the lesson: Step 1: Create the Expense Data
Our dataset is a month of personal expenses. Each entry has a date, a description, an amount, and a category. We'll also define monthly budget limits for each category.
Read this part of the lessonCreate an expense DataFrame from the provided dictionary and:
1. Convert the date column to datetime
2. Print the total number of expenses
3. Print the total amount spent (formatted to 2 decimal places)
4. Print the average expense amount
5. Print the most expensive single expense (description and amount)
Format:
Total expenses: 8
Total spent: $XXX.XX
Average expense: $XX.XX
Largest expense: DESCRIPTION ($XX.XX)import pandas as pd
expenses = {
'date': ['2024-03-01', '2024-03-05', '2024-03-08', '2024-03-12',
'2024-03-15', '2024-03-20', '2024-03-25', '2024-03-28'],
'description': ['Grocery store', 'Gas station', 'Restaurant',
'Phone bill', 'Gym', 'Movie tickets',
'Grocery store', 'Electric bill'],
'amount': [85.50, 45.00, 62.30, 65.00, 40.00, 28.00, 92.15, 120.00]
}
# Create DataFrame and print summary
More Budget Tracker exercises
- Categorize ExpensesIntermediate
- Spending Totals by CategoryIntermediate
- Budget ComparisonIntermediate
- Identify OverspendingIntermediate
- Complete Budget ReportIntermediate
All Python exercises · 50 practice problems · Practical Projects quiz