简体   繁体   中英

python pandas how to change value if it appears n consecutive times for exact id

I need to change values of a dataset if the value Positive appears more than 2 times in a row (to Negative) in my pandas dataframe, and i need to set id by id too, if it's a different id. If Negative breaks the more than 2 in a row cycle, or Negative appears more than 2 times in a row, nothing is done.

Example:

    id   status
0   3    Positive
1   3    Positive
2   3    Positive
3   2    Positive
4   1    Positive
5   2    Positive    
6   2    Positive
7   2    Positive

The resulting df should be:

    id   status
0   3    Positive
1   3    Positive
2   3    Negative
3   2    Positive
4   1    Positive
5   2    Positive    
6   2    Negative
7   2    Negative

We can use groupby().cumcount() to count the occurences of id , then np.where :

mask = (df['status'].eq('Positive')  # check for positive
            .groupby(df['id'])       # group by id
            .transform(lambda x:x.rolling(3).sum()) # count the consecutive positive in the last 3
            .eq(3)
       )
df.loc[mask, 'status'] = 'Negative'

Output:

   id    status
0   3  Positive
1   3  Positive
2   3  Negative
3   2  Positive
4   1  Positive
5   2  Positive
6   2  Negative
7   2  Negative

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