Search code examples
pythonpandasdataframecountrolling-computation

Pandas: conditional rolling count v.2


please help. I have dataframe:

   trade_type
0  -
1  Buy
2  -   
3  -
4  Sell
5  Buy
6  -
7  Sell
8  -
9  Sell
10 -

I need rolling count all that != "-" until next change and store it in each row in new column "trade_ID", so it looks like this:

 trade_type trade_ID
0  -        0
1  Buy      1
2  -        1
3  -        1
4  Sell     2
5  Buy      3
6  -        3
7  Sell     4
8  -        4
9  Sell     5
10  -       5

I tried to use:

df['trade_ID'] = (df.trade_type.shift(1) != df.trade_type).astype(int).cumsum()

but it counts "-" as new change so it doesn't work.


Solution

  • replace the - by np.nan (after import numpy as np) and filter on series.notna() and apply series.cumsum():

    df['trade_ID']=df.trade_type.replace("-",np.nan).notna().cumsum()
    print(df)
    
       trade_type  trade_ID
    0           -         0
    1         Buy         1
    2           -         1
    3           -         1
    4        Sell         2
    5         Buy         3
    6           -         3
    7        Sell         4
    8           -         4
    9        Sell         5
    10          -         5