I am trying to calculate the perc change occurring between two dates of the values for each id in my df:
date id a b
2021-01-01 1 6 4
2021-01-01 2 10 10
2021-01-02 1 3 2
2021-01-02 2 20 20
What I'd like to have as a result is:
id ratio_a ratio_b
1 -0.5 -0.5
2 1.0 1.0
I tried playing with pct_change()
but I cannot understand how to use it for this.
The data for testing can be generated with this code:
import datetime
import pandas as pd
dti = pd.to_datetime(
[
datetime.datetime(2021, 1, 1),
datetime.datetime(2021, 1, 1),
datetime.datetime(2021, 1, 2),
datetime.datetime(2021, 1, 2)
]
)
d = {
'id': [1, 2, 1, 2],
'a': [6, 10, 3, 20],
'b': [4, 10, 2, 20]
}
df = pd.DataFrame(data=d, index=dti)
I tried with df.groupby(by=['id']).pct_change()
and it gives me:
a b
2021-01-01 NaN NaN
2021-01-01 NaN NaN
2021-01-02 -0.5 -0.5
2021-01-02 1.0 1.0
, which is not exactly what I want. I would need a) that dates were sort of aggregated, and b) my ids.
question from:https://stackoverflow.com/questions/65936475/pandas-pct-change-using-time-series-and-preserving-ids