0
0
Pandasdata~15 mins

diff() for differences in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
Calculate Differences Using diff() in pandas
📖 Scenario: You work in a small shop that tracks daily sales numbers. You want to see how sales change from one day to the next.
🎯 Goal: Build a small program that uses pandas to find the daily difference in sales numbers using the diff() method.
📋 What You'll Learn
Create a pandas DataFrame with exact sales data for 5 days
Add a variable for the number of periods to calculate difference
Use the diff() method on the sales column with the periods variable
Print the DataFrame showing the original sales and the differences
💡 Why This Matters
🌍 Real World
Businesses often track changes in sales or other metrics day by day to understand trends and make decisions.
💼 Career
Data analysts and scientists use difference calculations to analyze time series data and detect patterns or anomalies.
Progress0 / 4 steps
1
Create the sales DataFrame
Create a pandas DataFrame called sales_data with a column 'Sales' containing these exact values: [100, 120, 115, 130, 125]. Use the index as days from 1 to 5.
Pandas
Need a hint?

Use pd.DataFrame with a dictionary for the column and set the index to days 1 through 5.

2
Set the periods variable
Create a variable called periods and set it to 1 to represent the difference between consecutive days.
Pandas
Need a hint?

Just create a variable named periods and assign it the value 1.

3
Calculate the daily sales difference
Create a new column in sales_data called 'Difference' by applying the diff() method on the 'Sales' column using the periods variable.
Pandas
Need a hint?

Use sales_data['Sales'].diff(periods=periods) and assign it to sales_data['Difference'].

4
Print the sales data with differences
Print the sales_data DataFrame to show the original sales and the calculated daily differences.
Pandas
Need a hint?

Use print(sales_data) to show the DataFrame with the new column.