iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
pct_change() measures how a value differs from an earlier observation, returning a fractional change; multiply the result by 100 to express it in percent units. cumsum() adds values in sequence to produce a running total. Both depend on row order, and grouped calculations keep each entity’s history separate.
How do pct_change() and cumsum() differ?
| Method | Question answered | Typical output | Key decision |
|---|---|---|---|
pct_change() |
How large is the change relative to an earlier observation? | A fraction such as 0.10 for a 10% increase |
Which prior period to compare with and how to handle missing values |
cumsum() |
What is the accumulated total so far? | A running sum | Which values, axis, order, and group boundary to use |
Use pct_change() to measure relative movement, not to calculate an accumulated amount. Use cumsum() for a running total, not just the final aggregate sum. In both cases, pandas follows the supplied row order unless you arrange the data first.
Calculate percent change with pct_change()
Despite its name, pct_change() returns fractional change (also called relative or per-unit change), not percentage units. The default compares each row with the immediately preceding row. The first row has no previous observation, so its result is missing. See the pandas 3.0.5 pct_change API documentation.
Free tools Windows power users keep installed
One-click scans. No signup required.
import pandas as pd
values = pd.Series([90, 91, 85])
change_fraction = values.pct_change()
change_percent = change_fraction * 100
print(change_fraction)
print(change_percent)
For the example values, the fractional changes are approximately 0.011111 and -0.065934; multiplying by 100 expresses them as approximately 1.1111% and -6.5934%. A negative result indicates a decrease. The fraction and percent are two ways to express the same relative change: do not multiply by 100 if downstream code expects fractional values.
#1 Best Overall
Choose the comparison interval
The periods argument controls how many rows back to compare. It defaults to 1; setting periods=2 compares each value with the one two rows earlier. The method also accepts freq for time-series index offsets. Choose the interval that matches the question, such as row-over-row change or a comparison across a longer interval.
# Compare each value with the value two rows earlier
change_over_two_rows = values.pct_change(periods=2)
Decide how missing observations should affect the comparison
Filling missing values changes the comparison baseline, so do it only when that treatment is appropriate for the data. For example, forward filling carries the last observed value into the missing row. Its change is then zero, and the next change is calculated against that carried-forward value.
Rank #2
# Carry the last observed value forward before calculating changes
change_after_fill = values_with_gaps.ffill().pct_change()
In current pandas documentation, fill_method for pct_change() must be None and is marked for removal in a future version. The pandas 2.1.0 release notes deprecated fill_method and limit, recommending explicit filling before the calculation. Older examples that pass fill_method='ffill' or rely on default filling are version-specific legacy usage; for current code, use an explicit preprocessing step such as .ffill() or, when suitable for the data, .bfill().
PC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteCalculate a running total with cumsum()
Series.cumsum() accumulates values through the series. DataFrame.cumsum() performs a cumulative sum along an axis. For a column running total, sort the rows into the intended sequence first, then apply the method:
# df is ordered by the date or sequence that defines the running total
df["running_total"] = df["value"].cumsum()
The result at each position reflects the values accumulated up to that position in the chosen order. Sorting is essential when the existing row order does not match the intended chronology or sequence. Do not assume a running total over unsorted rows represents a chronological total.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Keep calculations separate for each entity
When rows belong to independent products, accounts, sensors, or other entities, calculate changes and totals within each entity so one entity does not inherit another’s history. Sort by entity and sequence key before grouping when each group should be chronological:
df = df.sort_values(["entity", "date"])
groups = df.groupby("entity")["value"]
df["change_fraction"] = groups.pct_change()
df["running_total"] = groups.cumsum()
The pandas groupby guide lists both pct_change() and cumsum() as group operations. Built-in group operations can also be broadcast to the original grouped rows with transform when that form is useful.
The Tool Desk
Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →Quick Recap
Best Value
Common mistakes to avoid
- Reading a fraction as a percent:
0.10means a 10% increase; multiply by 100 to display it as10percent units. - Comparing with the wrong observation: set
periodsdeliberately if the previous row is not the intended baseline. - Filling gaps without considering the assumption: forward fill carries a prior value into the gap and changes later comparisons.
- Accumulating unsorted data: both adjacent-row change and running totals follow the order provided.
- Mixing independent histories: group by the entity when each entity needs its own change and running total.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

