Pandas DataFrame pct_change() Method
Example
Find the percentage difference between the values in current row and previous row:
import pandas as pd
data = [[10, 18, 11], [20, 15, 8], [30, 20, 3]]
df = pd.DataFrame(data)
print(df.pct_change())
Try it Yourself »
Definition and Usage
The pct_change()
method returns a DataFrame with
the percentage difference between the values for each row and, by default, the previous
row.
Which row to compare with can be specified with the
periods
parameter.
Syntax
dataframe.pct_change(periods, axis, fill_method, limit, freq,
kwargs)
Parameters
The
periods
, fill_method
,
axis
, limit
, freq
parameters are
keyword arguments..
Parameter | Value | Description |
---|---|---|
periods | a number | Optional. Specifies which row/column to calculate the difference between. Default 1, which means the previous row/column. |
axis | 0 |
Optional, default 0, specifies the axis to check the difference between. |
fill_method | String | Optional, default 'pad'. Specifies how to deal with NULL values. |
limit | None Number |
Optional, default None. Specifies how many NULL values to fill before ending the comparison. |
freq | Date String |
Optional, Specifies the increment to use for datetime values. |
Return Value
A DataFrame object with the differences.
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.