Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

apply reptitive sequence on multiindex dataframe

i have a multiindex dataframe where the inner most index can be of unequal length, I want to be able to add another column with repetitive values, but since the row counts are unequal, I am not able to so with:

df['marker'] = np.repeat([0,1,2], len(df), axis = 0)
ValueError: Length of values does not match length of index

Here is my dataframe sample:

                               close
date    ticker      expiry_dt   
2016-07-27  BHEL    2016-07-28  147
                    2016-08-25  147
                    2016-09-29  150
2016-07-28  BHEL    2016-07-28  149
                    2016-08-25  147
                    2016-09-29  149
2016-07-29  BHEL    2016-08-25  149
                    2016-09-29  149

as you can see, the innermost index ('expirty_dt') is of unequal length. My desired output is:

enter image description here

I can maybe achieve this through a loop, but I have a large database and loop will be inefficient in doing so on a daily basis. Thanks in advance

like image 935
Siraj S. Avatar asked Nov 27 '25 17:11

Siraj S.


1 Answers

you want

df.groupby(['date', 'ticker']).cumcount()
like image 115
piRSquared Avatar answered Nov 30 '25 05:11

piRSquared



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!