简体   繁体   中英

exclude row for rolling mean calculation in pandas

I am looking for Pandas way to solve this, I have a DataFrame as

df
         A     RM
    0  384    NaN
    1  376  380.0
    2  399  387.5
    3  333  366.0
    4  393  363.0
    5  323  358.0
    6  510  416.5
    7  426  468.0
    8  352  389.0 

I want to see if value in df['A'] > [Previous] RM value then new column Status should have 0 updated else

     A     RM  Status
0  384    NaN       0
1  376  380.0       1
2  399  387.5       0
3  333  366.0       1
4  393  363.0       0
5  323  358.0       1
6  510  416.5       0
7  426  468.0       0
8  352  389.0       1

I suppose i need to use Shift with numpy where , but I am not getting as desired.

import pandas as pd
import numpy as np
df=pd.DataFrame([384,376,399,333,393,323,510,426,352], columns=['A'])


df['RM']=df['A'].rolling(window=2,center=False).mean()

df['Status'] =  np.where((df.A > df.RM.shift(1).rolling(window=2,center=False).mean()) , 0, 1) 

Finally, applying rolling mean

df.AverageMean=df[df['Status'] == 1]['A'].rolling(window=2,center=False).mean()

Just simple shift

df['Status']=(df.A<=df.RM.fillna(9999).shift()).astype(int)
df
Out[347]: 
     A     RM  Status
0  384    NaN       0
1  376  380.0       1
2  399  387.5       0
3  333  366.0       1
4  393  363.0       0
5  323  358.0       1
6  510  416.5       0
7  426  468.0       0
8  352  389.0       1

i assume when you compare with na it always be 1

df['Status'] = (df.A < df.RM.fillna(df.A.max()+1).shift(1)).astype(int)


    A   RM  Status
0   384 NaN     0
1   376 380.0   1
2   399 387.5   0
3   333 366.0   1
4   393 363.0   0
5   323 358.0   1
6   510 416.5   0
7   426 468.0   0
8   352 389.0   1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM