简体   繁体   中英

Python DataFrame : Remove rows based on condition?

I have a df with two columns name and score . I'm trying to keep only the rows for each user where score > 1 starts.

    df

            name       score     
    0       bruno        0         
    1       bruno        0         
    2       bruno        15        
    3       bruno        0        
    4       paul         0         
    5       paul         0          
    6       paul         25
    7       paul         0
    8       paul         10
    9       marcus        5
    10      mason         0

final df

            name       score            
    2       bruno        15        
    3       bruno        0                 
    6       paul         25
    7       paul         0
    8       paul         10
    9       marcus        5 
x = df[df.groupby("name")["score"].cumsum().gt(0)]
print(x)

Prints:

     name  score
2   bruno     15
3   bruno      0
6    paul     25
7    paul      0
8    paul     10
9  marcus      5

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