I have a data frame which contains dates as index and a value column storing growth percentage between consecutive dates (i.e. dates in the index). Suppose I want to compute 'real' values by setting a 100 basis at the first date of the index and then iteratively applying the % of growth. It is easy with the cumprod method.
Now, I want to set as 100 basis the laste date in the index. I thus need to compute for each date in the index the 'inverse' growth. Is there an easy way (and pythonic) to do this with pandas?
Regards,
Allia
Consider the dataframe df
with random returns for 10 days.
np.random.seed([3,1415])
tidx = pd.date_range('2012-04-01', periods=10)
df = pd.DataFrame(dict(A=np.random.rand(10) / 10), tidx)
df
A
2012-04-01 0.044494
2012-04-02 0.040755
2012-04-03 0.046015
2012-04-04 0.046524
2012-04-05 0.046269
2012-04-06 0.001655
2012-04-07 0.085045
2012-04-08 0.081774
2012-04-09 0.077796
2012-04-10 0.075798
You want to use cumprod
then divide by the last value
dcum = df.add(1).cumprod()
dcum.div(dcum.iloc[-1]).mul(100)
A
2012-04-01 61.539104
2012-04-02 64.047157
2012-04-03 66.994277
2012-04-04 70.111111
2012-04-05 73.355090
2012-04-06 73.476459
2012-04-07 79.725230
2012-04-08 86.244715
2012-04-09 92.954225
2012-04-10 100.000000
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With