I want to calculate the percentage change for the following data frame.
import pandas as pd
df = pd.DataFrame({'team': ['A', 'A', 'A', 'B', 'B', 'B', 'C', 'C'],
'points': [12, 0, 19, 22, 0, 25, 0, 30],
'score': [12, 0, 19, 22, 0, 25, 0, 30]
})
print(df)
When I applied this step, it returns inf which is obvious because we are dividing by zero.
df['score'] = df.groupby('team', sort=False)['score'].apply(
lambda x: x.pct_change()).to_numpy()
But if we see in each column the change from 0 to 19 the change is 100%, from 0 to 25 the change is 100%, and from 0 to 30 the change is 100%. So, I was wondering how can I calculate those values.