liveBook · Manning

Pct_change

As explained in Pandas Workout: 200 exercises to make you a stronger data analyst

pct_change is a pandas method that computes fractional or percentage changes between successive values along an axis.

Think of pct_change as asking how much the current value moved relative to the value immediately before it. It returns fractional changes, so 0.5 represents a 50% increase. section 7.4

Comparing successive sales values

For sales of 100, 150, 200, and 250, df['sales'].pct_change() returns NaN, 0.500000, 0.333333, and 0.250000. The first position is NaN because no earlier value exists; later results use (later − earlier) / earlier. section 7.4

Use pct_change effectively by aligning the data so that the rows represent the comparisons you intend:

  • Transpose with .T when the values to compare are arranged in columns, because pct_change compares rows in this workflow. section 7.6
  • Use diff instead when you need raw changes rather than relative or percentage changes. section 7.4

How Pandas Workout: 200 exercises to make you a stronger data analyst covers it

How deep it goes
Explains the formula, initial NaN, row orientation, transposition, and contrast with diff, without covering implementation internals.
Examples
Uses sequential sales values and grouped SAT math scores across income brackets and years.
What it assumes
Assumes familiarity with pandas Series, DataFrames, grouping, means, indexes, and transposition while teaching the comparison formula from its first result.
What you can do after
Apply it via pandas to calculate successive relative changes and compare ordered rows after reshaping data when necessary.
How it connects
Connects pct_change to rolling window functions, expanding window functions, diff, groupby, mean, sorting, and transpose.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pandas Workout: 200 exercises to make you a stronger data analyst. The section links point to the parts of the book it draws on.