Pandas Series: pct_change() function
Percentage change between the current and a prior element
The pct_change() function is used to get percentage change between the current and a prior element.
Computes the percentage change from the immediately previous row by default. This is useful in comparing the percentage of change in a time series of elements.
Syntax:
Series.pct_change(self, periods=1, fill_method='pad', limit=None, freq=None, **kwargs)
Parameters:
Name | Description | Type/Default Value | Required / Optional |
---|---|---|---|
periods | Periods to shift for forming percent change. | int Default Value: 1 |
Required |
fill_method | How to handle NAs before computing percent changes. | str Default Value: ‘pad’ |
Required |
limit | The number of consecutive NAs to fill before stopping. | int Default Value: None |
Required |
freq | Increment to use from time series API (e.g. ‘M’ or BDay()). | DateOffset, timedelta, or offset alias string | Optional |
**kwargs | Additional keyword arguments are passed into DataFrame.shift or Series.shift. | Required |
Returns: chg - Series or DataFrame
The same type as the calling object.
Example - Series:
Python-Pandas Code:
import numpy as np
import pandas as pd
s = pd.Series([80, 81, 75])
s
Output:
0 80 1 81 2 75 dtype: int64
Python-Pandas Code:
import numpy as np
import pandas as pd
s = pd.Series([80, 81, 75])
s.pct_change()
Output:
0 NaN 1 0.012500 2 -0.074074 dtype: float64
Python-Pandas Code:
import numpy as np
import pandas as pd
s = pd.Series([80, 81, 75])
s.pct_change(periods=2)
Output:
0 NaN 1 NaN 2 -0.0625 dtype: float64
Example - See the percentage change in a Series where filling NAs with last valid observation forward to next valid:
Python-Pandas Code:
import numpy as np
import pandas as pd
s = pd.Series([80, 81, None, 75])
s
Output:
0 80.0 1 81.0 2 NaN 3 75.0 dtype: float64
Python-Pandas Code:
import numpy as np
import pandas as pd
s = pd.Series([80, 81, None, 75])
s.pct_change(fill_method='ffill')
Output:
0 NaN 1 0.012500 2 0.000000 3 -0.074074 dtype: float64
Example - DataFrame:
Percentage change in French franc, Deutsche Mark, and Italian lira from 2000-01-01 to 2000-03-01
Python-Pandas Code:
import numpy as np
import pandas as pd
df = pd.DataFrame({
'FR': [5.0505, 5.0963, 5.3149],
'GR': [2.7246, 2.7482, 2.8519],
'IT': [904.74, 910.01, 960.13]},
index=['2000-01-01', '2000-02-01', '2000-03-01'])
df
Output:
FR GR IT 2000-01-01 5.0505 2.7246 904.74 2000-02-01 5.0963 2.7482 910.01 2000-03-01 5.3149 2.8519 960.13
Python-Pandas Code:
import numpy as np
import pandas as pd
df = pd.DataFrame({
'FR': [5.0505, 5.0963, 5.3149],
'GR': [2.7246, 2.7482, 2.8519],
'IT': [904.74, 910.01, 960.13]},
index=['2000-01-01', '2000-02-01', '2000-03-01'])
df.pct_change()
Output:
FR GR IT 2000-01-01 NaN NaN NaN 2000-02-01 0.009068 0.008662 0.005825 2000-03-01 0.042894 0.037734 0.055076
Example - Percentage of change in GOOG and APPL stock volume. Shows computing the percentage change between columns:
Python-Pandas Code:
import numpy as np
import pandas as pd
df = pd.DataFrame({
'2019': [1869950, 32586265],
'2018': [1600923, 42912316],
'2017': [1471819, 42403351]},
index=['GOOG', 'APPL'])
df
Output:
2019 2018 2017 GOOG 1869950 1600923 1471819 APPL 32586265 42912316 42403351
Python-Pandas Code:
import numpy as np
import pandas as pd
df = pd.DataFrame({
'2019': [1869950, 32586265],
'2018': [1600923, 42912316],
'2017': [1471819, 42403351]},
index=['GOOG', 'APPL'])
df.pct_change(axis='columns')
Output:
2019 2018 2017 GOOG NaN -0.143869 -0.080643 APPL NaN 0.316884 -0.011861
Previous: Get the smallest n elements in Pandas
Next: Product of the values for the requested Pandas axis
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/pandas/series/series-pct_change.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics