I would like to add a column to a Pandas dataframe and set values based on index levels 0 and 1. For example, set the value to buy for indexes where level 0 is hom and column delta is greater than 0. And set the value to sell for indexes where level 0 is hom and column delta is lower than 0. There are other rules for different value of level 0.
How can I do that ?
>df
delta
fut ABC 15284.233222
pos DEF 0.248976
POL 0.002041
ABC 0.043585
hom YTY 0.054100
MNN -0.356873
This is the desired output:
>df
delta new_col
fut ABC 15284.23 nan
pos DEF 0.248976 nan
POL 0.002041 nan
ABC 0.043585 nan
hom YTY 0.054100 buy
MNN -0.356873 sell
I can filter the dataframe with loc for not sure how to create the new column.
df.loc[df.index.get_level_values(level=0) == 'hom'] > 0
delta new_col
hom YTY 0.054100 True
MNN -0.356873 False