It calculates the percentage change between the current and a prior element in a pandas Series or DataFrame. This helps to see how much values have increased or decreased in percentage terms.
Click to reveal answer
beginner
How do you calculate the percentage change between rows in a DataFrame using pandas?
Use df.pct_change(). By default, it calculates the percentage change between the current row and the previous row for each column.
Click to reveal answer
intermediate
What does the periods parameter in pct_change() control?
It controls how many rows (or periods) back to compare the current value to. For example, periods=2 compares the current row to the row two steps before.
Click to reveal answer
intermediate
If a value in the previous row is zero, what will pct_change() return for that row?
It will return inf (infinity) or NaN because division by zero is undefined in percentage change calculation.
Click to reveal answer
intermediate
How can you use pct_change() to calculate monthly percentage changes in a time series DataFrame?
Set the DataFrame index to a datetime type and use df.pct_change(periods=1) if data is monthly. For other frequencies, adjust periods accordingly.
Click to reveal answer
What is the default number of periods pct_change() compares when calculating percentage change?
A0
B1
C2
D-1
✗ Incorrect
By default, pct_change() compares the current row with the previous row, which means periods=1.
If a DataFrame column has values [100, 110, 121], what is the percentage change from the first to the second value?
From 100 to 120: (120-100)/100 = 0.2 From 120 to 150: (150-120)/120 = 0.25 From 150 to 180: (180-150)/150 = 0.2
Step 2: Understand first row result
The first row has no previous row, so result is NaN.
Final Answer:
NaN, 0.2, 0.25, 0.2 -> Option D
Quick Check:
pct_change() = NaN, 0.2, 0.25, 0.2 [OK]
Hint: First row is always NaN; next rows show percent change [OK]
Common Mistakes:
Confusing absolute change with percent change
Forgetting first row is NaN
Mixing up order of results
4. What is wrong with this code snippet?
import pandas as pd
df = pd.DataFrame({'A': [10, 20, 30]})
result = df.pct_change(axis=1)
print(result)
medium
A. The DataFrame has no numeric columns.
B. The code is correct and will run without errors.
C. pct_change() requires a parameter 'periods' to work.
D. pct_change() cannot be used with axis=1 on this DataFrame.
Solution
Step 1: Understand axis parameter in pct_change()
By default, pct_change() calculates percentage change down rows (axis=0). axis=1 calculates change across columns.
Step 2: Check DataFrame shape and axis=1 effect
The DataFrame has only one column. pct_change(axis=1) produces NaN values everywhere since there are no multiple columns to compare, but the code runs without errors or exceptions.
Final Answer:
The code is correct and will run without errors. -> Option B
Quick Check:
axis=1 on single column = NaN output, no error [OK]
Hint: axis=1 on single column gives NaN but runs fine [OK]
Common Mistakes:
Thinking axis=1 causes error on single-column DataFrame
Believing periods parameter is required
Claiming no numeric columns
5. You have a DataFrame df with daily stock prices:
You want to calculate the percentage change over 2 days instead of 1. Which code will give the correct result?
hard
A. df.pct_change(axis=1)
B. df.pct_change(periods=1)
C. df.pct_change(periods=2)
D. df.pct_change(periods=0)
Solution
Step 1: Understand the periods parameter
The periods parameter in pct_change() sets how many rows to look back for percentage change calculation. For 2-day change, use periods=2.
Step 2: Check other options
df.pct_change(periods=1) calculates 1-day change, df.pct_change(axis=1) uses axis=1 which is incorrect for this data, and df.pct_change(periods=0) computes zero change.
Final Answer:
df.pct_change(periods=2) -> Option C
Quick Check:
periods=2 for 2-day change [OK]
Hint: Use periods=2 to compare with 2 rows before [OK]
Common Mistakes:
Using default periods=1 for 2-day change
Using axis=1 incorrectly
Setting periods=0 which is invalid
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.