Filter rows after groupby pandas

前端 未结 4 2008
耶瑟儿~
耶瑟儿~ 2021-01-05 09:58

I have a table in pandas:

import pandas as pd

df = pd.DataFrame({
    \'LeafID\':[1,1,2,1,3,3,1,6,3,5,1],
    \'pidx\':[10,10,300,10,30,40,20,10,30,45,20],         


        
4条回答
  •  佛祖请我去吃肉
    2021-01-05 10:35

    This is a straightforward application of filter after doing a groupby. In the data you provided, a value of 20 for pidx only occurred twice so it was filtered out.

    df.groupby('pidx').filter(lambda x: len(x) > 2)
    
       LeafID  count  pidx  pidy
    0       1     10    10    20
    1       1     20    10    20
    3       1     40    10    20
    7       6     50    10    43
    

提交回复
热议问题