Pandas groupby take counts greater than 1

前端 未结 1 368
忘掉有多难
忘掉有多难 2021-01-06 06:58

This must be a simple question, but, however, it is bugging my head for a while.

For a dataframe below:

df = pd.DataFrame({\'c0\': [\'a\',\'b\',\'a\'         


        
1条回答
  •  囚心锁ツ
    2021-01-06 07:37

    Use GroupBy.transform for Series with same size like original DataFrame:

    df1 = df[df.groupby(['c0','c1'])['c2'].transform('count') > 1]
    

    Or use DataFrame.duplicated for filtered all dupe rows by specified columns in list:

    df1 = df[df.duplicated(['c0','c1'], keep=False)]
    

    If performance is in not important or small DataFrame use DataFrameGroupBy.filter:

    df1 = df.groupby(['c0','c1']).filter(lambda x: len(x) > 1)
    

    0 讨论(0)
提交回复
热议问题