Computing Percent Change for Each Group in a Pandas DataFrame Using GroupBy and PctChange
Computing Percent Change for Each Group in a DataFrame
To compute percent change for each group in the Name column of a DataFrame, you can use the groupby method along with the pct_change function.
Code Example
import pandas as pd
import numpy as np
# Sample data
d = {'Name': ['AAL', 'AAL', 'AAL', 'AAL', 'AAL', 'TST', 'TST', 'TST'],
'close': [14.75, 14.46, 14.27, 14.66, 13.99, 10, 11, 22],
'date': [pd.Timestamp('2013-02-08'), pd.Timestamp('2013-02-11'), pd.Timestamp('2013-02-12'),
pd.Timestamp('2013-02-13'), pd.Timestamp('2013-02-14'),
pd.Timestamp('2013-02-11'), pd.Timestamp('2013-02-12'), pd.Timestamp('2013-02-13')],
'high': [15.12, 15.01, 14.51, 14.94, 14.96, 11, 12, 23],
'low': [14.63, 14.26, 14.1, 14.25, 13.16, 9, 10, 21],
'open': [15.07, 14.89, 14.45, 14.3, 14.94, 10, 11, 22],
'volume': [8407500, 8882000, 8126000, 10259500, 31879900, 8126000, 10259500, 31879900]}
df = pd.DataFrame(d)
# Compute percent change for each group in the Name column
df['ret'] = df.groupby('Name')['close'].pct_change()
print(df)
Output
The groupby method groups the data by the Name column and returns a GroupBy object. The pct_change function calculates the percent change for each group in the close column.
The resulting DataFrame, df, includes an additional ret column containing the computed percent changes.
Note that the first row of each group has a NaN value in the ret column because there is no previous value to calculate the percent change from.
Last modified on 2023-08-06