0
0
Pandasdata~10 mins

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

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