Bird
Raised Fist0
Pandasdata~20 mins

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

Choose your learning style10 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.

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

  1. 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.
  2. 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.
  3. Final Answer:

    Calculates the difference between each value and the previous value in the column -> Option A
  4. 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

  1. Step 1: Identify correct method call on a DataFrame column

    To calculate differences on a column, call diff() on the Series: df['sales'].diff().
  2. 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.
  3. Final Answer:

    df['sales'].diff() -> Option B
  4. Quick Check:

    Correct syntax = df['column'].diff() [OK]
Hint: Use diff() directly on the column Series like df['col'].diff() [OK]
Common Mistakes:
  • Passing column name as argument to diff()
  • Using square brackets instead of parentheses
  • Calling non-existent methods like difference()
3. Given the DataFrame:
import pandas as pd
df = pd.DataFrame({'A': [10, 15, 20, 18]})
df['diff'] = df['A'].diff()

What is the content of the diff column?
medium
A. [NaN, 10, 15, 20]
B. [10, 15, 20, 18]
C. [NaN, 5.0, 5.0, -2.0]
D. [0, 5, 5, -2]

Solution

  1. Step 1: Calculate differences between consecutive values in column 'A'

    Values: 10, 15, 20, 18. Differences: NaN (first row), 15-10=5, 20-15=5, 18-20=-2.
  2. Step 2: Match calculated differences with options

    [NaN, 5.0, 5.0, -2.0] matches the list [NaN, 5.0, 5.0, -2.0]. Others do not.
  3. Final Answer:

    [NaN, 5.0, 5.0, -2.0] -> Option C
  4. Quick Check:

    diff() = [NaN, 5, 5, -2] [OK]
Hint: First diff is always NaN; subtract previous value from current [OK]
Common Mistakes:
  • Assuming first diff is zero instead of NaN
  • Confusing original values with differences
  • Ignoring negative differences
4. You run this code:
import pandas as pd
df = pd.DataFrame({'X': [3, 7, 2]})
df['change'] = df.diff(axis=1)

But you get an error or unexpected output. What is the problem?
medium
A. diff() cannot be used with axis=1 on a single-column DataFrame
B. You must specify the period argument in diff()
C. The DataFrame must have numeric column names for diff()
D. diff() requires the DataFrame to have at least 5 rows

Solution

  1. Step 1: Understand axis parameter in diff()

    axis=1 means calculate difference across columns (left to right). With only one column, no previous column exists to subtract.
  2. Step 2: Identify why error or unexpected output occurs

    Since only one column exists, diff(axis=1) returns NaN or error because no previous column to compare.
  3. Final Answer:

    diff() cannot be used with axis=1 on a single-column DataFrame -> Option A
  4. Quick Check:

    diff(axis=1) needs multiple columns [OK]
Hint: diff(axis=1) needs 2+ columns; single column causes NaN/error [OK]
Common Mistakes:
  • Thinking period argument is mandatory
  • Assuming column names must be numeric
  • Believing diff() needs many rows
5. You have a DataFrame showing daily temperatures:
import pandas as pd
data = {'Day': ['Mon', 'Tue', 'Wed', 'Thu'], 'Temp': [20, 22, 21, 25]}
df = pd.DataFrame(data)

You want to add a column Temp_change showing how much temperature changed from the previous day. Which code correctly does this?
hard
A. df['Temp_change'] = df['Temp'].cumsum()
B. df['Temp_change'] = df['Temp'].diff(periods=2)
C. df['Temp_change'] = df['Temp'].shift()
D. df['Temp_change'] = df['Temp'].diff()

Solution

  1. Step 1: Identify how to calculate daily temperature change

    Temperature change from previous day is difference between current and previous value, which diff() calculates by default with period=1.
  2. Step 2: Evaluate each option

    df['Temp_change'] = df['Temp'].diff() uses diff() correctly. df['Temp_change'] = df['Temp'].cumsum() sums temperatures, not differences. df['Temp_change'] = df['Temp'].shift() shifts values but does not calculate difference. df['Temp_change'] = df['Temp'].diff(periods=2) calculates difference over 2 days, not 1.
  3. Final Answer:

    df['Temp_change'] = df['Temp'].diff() -> Option D
  4. Quick Check:

    Use diff() for stepwise changes [OK]
Hint: Use diff() without arguments for previous row difference [OK]
Common Mistakes:
  • Using cumsum() instead of diff()
  • Using shift() which only moves data
  • Using diff(periods=2) for daily change
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard