📖 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
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
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
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
Hint
Use print(sales_data) to show the DataFrame with the new column.
Practice
(1/5)
1. What does the diff() function in pandas do when applied to a DataFrame column?
easy
A. Calculates the difference between each value and the previous value in the column
B. Calculates the sum of all values in the column
C. Sorts the column values in ascending order
D. Replaces missing values with zeros
Solution
Step 1: Understand the purpose of diff()
The diff() function calculates the difference between each element and the one before it in the same column.
Step 2: Compare options with the function's behavior
Only Calculates the difference between each value and the previous value in the column correctly describes this behavior. Other options describe different functions.
Final Answer:
Calculates the difference between each value and the previous value in the column -> Option A
Quick Check:
diff() = difference between consecutive values [OK]
Hint: Remember, diff() shows step-by-step changes in data [OK]
Common Mistakes:
Thinking diff() sums values
Confusing diff() with sorting
Assuming diff() fills missing data
2. Which of the following is the correct syntax to calculate the difference between rows in a pandas DataFrame column named sales?
easy
A. df.diff('sales')
B. df['sales'].diff()
C. df.diff[sales]()
D. df['sales'].difference()
Solution
Step 1: Identify correct method call on a DataFrame column
To calculate differences on a column, call diff() on the Series: df['sales'].diff().
Step 2: Check syntax of each option
df['sales'].diff() uses correct syntax. df.diff('sales') wrongly passes column name to diff. df.diff[sales]() uses wrong brackets. df['sales'].difference() uses a non-existent method.
Final Answer:
df['sales'].diff() -> Option B
Quick Check:
Correct syntax = df['column'].diff() [OK]
Hint: Use diff() directly on the column Series like df['col'].diff() [OK]