I have a pandas data frame, where datetime is the index of the data frame (I use t=0 for simplification, in fact there is something like 20170101 09:30:00)
datetime Stock A Stock B
t=0 5 20
t=1 6 30
t=2 8 25
t=3 4 20
and I would like to return:
datetime Stock A Stock B
t=0 100 100
t=1 120 150
t=2 140 125
t=3 80 100
in mathematical terms: Index(i, t) = P(i, t) / P(i, 0).
I tried
df_norm = df[0:] / df[0:1]
print(df_norm)
which gives me an error.
edit1: I tried option 3 which works fine (couldn't try on NaN's yet, but at least it does not create an NaN for the first obs (which is caused by pctchange)). I wonder also that after performing, my datetime is not the set index anymore, which is easy to fix by just re-assigning it.
Now I am trying now to wrap it in a function, but I think the index is causing a problem (actually same error as with my "first" attempt):
def norming(x):
return x.assign(**x.drop('datetime', 1).pipe(
lambda d: d.div(d.shift().bfill()).cumprod()))
edit2: if my column datetime is an index, i.e.
df_norm.set_index(['datetime'], inplace = True)
I'll get an error though, what would I need to change?