Bird
Raised Fist0
Pandasdata~10 mins

pct_change() for percentage change in Pandas - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - pct_change() for percentage change
Start with DataFrame
↓
Select column or series
↓
Calculate difference between current and previous value
↓
Divide difference by previous value
↓
Return new series with percentage changes
↓
Handle NaN for first element
↓
Output percentage change series
pct_change() calculates the percent difference between each value and the previous one in a column or series, returning a new series with these percentage changes.
Execution Sample
Pandas
import pandas as pd
s = pd.Series([100, 120, 90, 150])
result = s.pct_change()
print(result)
This code calculates the percentage change between each number and the previous number in the series.
Execution Table
StepCurrent ValuePrevious ValueDifferencePercentage ChangeOutput Value
1100NaNNaNNaNNaN
2120100200.200.20
390120-30-0.25-0.25
415090600.66670.6667
5End---Calculation complete
💡 Reached end of series; no more values to compare.
Variable Tracker
VariableStartAfter 1After 2After 3Final
s[100, 120, 90, 150][100, 120, 90, 150][100, 120, 90, 150][100, 120, 90, 150][100, 120, 90, 150]
result[NaN, NaN, NaN, NaN][NaN, 0.20, NaN, NaN][NaN, 0.20, -0.25, NaN][NaN, 0.20, -0.25, 0.6667][NaN, 0.20, -0.25, 0.6667]
Key Moments - 3 Insights
Why is the first output value NaN?
The first value has no previous value to compare to, so pct_change() returns NaN for it, as shown in step 1 of the execution_table.
How is the percentage change calculated for step 3?
At step 3, difference is 90 - 120 = -30. Percentage change is -30 divided by 120 = -0.25, as shown in the execution_table.
What does a negative percentage change mean?
A negative percentage change means the current value is less than the previous value, indicating a decrease, as seen at step 3 with -0.25.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the percentage change at step 4?
A-0.25
B0.25
C0.6667
DNaN
💡 Hint
Check the 'Percentage Change' column at step 4 in the execution_table.
At which step does the percentage change become negative?
AStep 2
BStep 3
CStep 4
DNever
💡 Hint
Look for the first negative value in the 'Percentage Change' column in the execution_table.
If the first value in the series was 0 instead of 100, what would be the percentage change at step 2?
AInfinite or error
BNaN
C0.20
D-0.20
💡 Hint
Recall that percentage change divides by the previous value; dividing by zero causes an error or infinite result.
Concept Snapshot
pct_change() calculates the percent difference between each value and the previous one in a series or DataFrame column.
Syntax: series.pct_change()
Returns a new series with NaN for the first element (no previous value).
Useful to see relative changes over time or between rows.
Handles positive and negative changes as decimals (e.g., 0.20 = 20%).
Full Transcript
The pct_change() function in pandas calculates the percentage change between each value and the previous value in a series or DataFrame column. It starts by taking the first value, which has no previous value, so it returns NaN for that position. Then for each next value, it subtracts the previous value from the current value, divides the difference by the previous value, and returns this as the percentage change. For example, if the current value is 120 and the previous is 100, the difference is 20, and the percentage change is 20 divided by 100, which is 0.20 or 20%. Negative values mean the current value is less than the previous, showing a decrease. This function helps to understand how data changes step-by-step in relative terms.

Practice

(1/5)
1. What does the pct_change() function in pandas do?
easy
A. Calculates the percentage change between consecutive rows.
B. Calculates the sum of all values in a column.
C. Sorts the DataFrame by index.
D. Removes missing values from the DataFrame.

Solution

  1. Step 1: Understand the purpose of pct_change()

    The pct_change() function calculates how much values change in percentage from one row to the next.
  2. Step 2: Compare with other options

    Options A, B, and C describe different pandas functions unrelated to percentage change.
  3. Final Answer:

    Calculates the percentage change between consecutive rows. -> Option A
  4. Quick Check:

    pct_change() = percentage change [OK]
Hint: Think: pct_change means percent change between rows [OK]
Common Mistakes:
  • Confusing pct_change() with sum()
  • Thinking it sorts data
  • Assuming it removes missing values
2. Which of the following is the correct way to use pct_change() on a pandas DataFrame named df?
easy
A. df.pct_change()
B. pct_change(df)
C. df.change_pct()
D. df.percent_change()

Solution

  1. Step 1: Recall pandas method syntax

    In pandas, pct_change() is a method called on a DataFrame or Series object, so the correct syntax is df.pct_change().
  2. Step 2: Check other options

    Options A, B, and D are not valid pandas syntax or method names.
  3. Final Answer:

    df.pct_change() -> Option A
  4. Quick Check:

    Method called on DataFrame = df.pct_change() [OK]
Hint: Remember: pandas methods use dot notation like df.method() [OK]
Common Mistakes:
  • Using pct_change() as a standalone function
  • Mixing method names
  • Using incorrect method names
3. Given the DataFrame:
import pandas as pd
df = pd.DataFrame({'Sales': [100, 120, 150, 180]})
print(df.pct_change())

What is the output?
medium
A. 0.2, 0.25, 0.2, NaN
B. 0, 20, 30, 30
C. NaN, 20, 30, 30
D. NaN, 0.2, 0.25, 0.2

Solution

  1. Step 1: Calculate percentage changes row by row

    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
  2. Step 2: Understand first row result

    The first row has no previous row, so result is NaN.
  3. Final Answer:

    NaN, 0.2, 0.25, 0.2 -> Option D
  4. 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

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

    The code is correct and will run without errors. -> Option B
  4. 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:
import pandas as pd
df = pd.DataFrame({'Price': [50, 52, 51, 53, 54]})

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

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

    df.pct_change(periods=2) -> Option C
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard