Pandas: Selecting rows for which groupby.sum() satisfies condition

前端 未结 3 768
轻奢々
轻奢々 2021-01-19 05:49

In pandas I have a dataframe of the form:

>>> import pandas as pd  
>>> df = pd.DataFrame({\'ID\':[51,51,51,24,24,24,31], \'x\':[0,1,0,0,1,         


        
3条回答
  •  广开言路
    2021-01-19 06:04

    df = pd.DataFrame({'ID':[51,51,51,24,24,24,31], 'x':[0,1,0,0,1,1,0]})
    df.loc[df.groupby(['ID'])['x'].transform(func=sum)>=2,:]
    out:
       ID  x
    3  24  0
    4  24  1
    5  24  1
    

提交回复
热议问题