I have a multi-index dataframe dfu
:
open high low close
Date Time
2016-11-28 09:43:00 26.03 26.03 26.030 26.030
09:48:00 25.90 25.90 25.760 25.760
09:51:00 26.00 26.00 25.985 25.985
2016-11-29 09:30:00 24.98 24.98 24.98 24.9800
09:33:00 25.00 25.00 24.99 24.9900
09:35:00 25.33 25.46 25.33 25.4147
I would like to create a new column, ['closeScaled'] that is calculated by doing a function, foo, using the first row of the current level=0 value from the ['open'] column and the current row['close'] as arguments. I suspect the solution will involve something looking like:
dfu['closeScaled']=dfu.apply(lambda x: foo(*get first row of current date*[0],x[3]))
I just can't seem to figure out the get first row of current level=0 part.
if foo
is:
def foo(firstOpen,currentClose):
return (currentClose / firstOpen)
then I would expect the closeScaled
column to contain (truncating to 4 decimals):
open high low close closeScaled
Date Time
2016-11-28 09:43:00 26.03 26.03 26.030 26.030 1.0000
09:48:00 25.90 25.90 25.760 25.760 0.9896
09:51:00 26.00 26.00 25.985 25.985 0.9982
2016-11-29 09:30:00 24.98 24.98 24.98 24.9800 1.0000
09:33:00 25.00 25.00 24.99 24.9900 1.0004
09:35:00 25.33 25.46 25.33 25.4147 1.0174