0
0
Pandasdata~20 mins

diff() for differences in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Diff Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of diff() with default parameters
What is the output of the following code snippet using pandas diff() with default settings?
Pandas
import pandas as pd
s = pd.Series([10, 15, 20, 25])
result = s.diff()
print(result)
A
0    NaN
1    10.0
2    15.0
3    20.0
dtype: float64
B
0    NaN
1    5.0
2    5.0
3    5.0
dtype: float64
C
0    10.0
1    5.0
2    5.0
3    5.0
dtype: float64
D
0    NaN
1    15.0
2    20.0
3    25.0
dtype: float64
Attempts:
2 left
💡 Hint
Remember diff() calculates the difference between the current and previous element by default.
data_output
intermediate
2:00remaining
Effect of periods parameter in diff()
Given the DataFrame below, what is the output of df['A'].diff(periods=2)?
Pandas
import pandas as pd
df = pd.DataFrame({'A': [3, 8, 15, 24, 35]})
result = df['A'].diff(periods=2)
print(result)
A
0     NaN
1     NaN
2    12.0
3    16.0
4    20.0
dtype: float64
B
0     NaN
1     NaN
2    15.0
3    24.0
4    35.0
dtype: float64
C
0     NaN
1     5.0
2     7.0
3     9.0
4    11.0
dtype: float64
D
0     NaN
1     NaN
2     8.0
3    15.0
4    24.0
dtype: float64
Attempts:
2 left
💡 Hint
The periods parameter defines how many rows back to subtract from the current row.
🔧 Debug
advanced
2:00remaining
Identify the error in diff() usage
What error will the following code produce and why?
Pandas
import pandas as pd
s = pd.Series([1, 2, 3])
result = s.diff(periods='two')
print(result)
ANo error, outputs the difference with periods=2
BTypeError: unsupported operand type(s) for -: 'int' and 'str'
CSyntaxError: invalid syntax
DValueError: periods must be an integer
Attempts:
2 left
💡 Hint
Check the type of the periods argument.
🚀 Application
advanced
2:00remaining
Using diff() to find daily sales changes
You have a DataFrame with daily sales data. Which code snippet correctly adds a new column 'daily_change' showing the difference in sales from the previous day?
Pandas
import pandas as pd
df = pd.DataFrame({'date': ['2024-01-01', '2024-01-02', '2024-01-03'], 'sales': [100, 150, 130]})
Adf['daily_change'] = df['sales'].diff(axis=1)
Bdf['daily_change'] = df['sales'].diff(periods=0)
Cdf['daily_change'] = df['sales'].diff()
Ddf['daily_change'] = df['sales'].diff(periods=-1)
Attempts:
2 left
💡 Hint
Default diff() compares current row with previous row.
🧠 Conceptual
expert
2:00remaining
Understanding diff() behavior with non-numeric data
What happens when you apply diff() on a pandas Series containing strings?
Pandas
import pandas as pd
s = pd.Series(['a', 'b', 'c'])
result = s.diff()
ARaises TypeError because subtraction is not supported for strings
BReturns the original Series unchanged
CReturns the difference in ASCII values between consecutive characters
DReturns a Series of NaN values
Attempts:
2 left
💡 Hint
diff() uses subtraction internally.