首页 > 解决方案 > 根据中间数据框删除行

问题描述

df = 

freq     id
  11      a
  11      b
  10      c
  9       d
  1       e
  1       f

我想查看每个值的freq存储次数,如果记录一次,请将其删除。期望的输出:

  count = 

freq     recordings
  11      2
  10      1
   9      1
   1      2

进而

df = 

freq     id
  11      a
  11      b
   1      e 
   1      f

标签: pythonpandas

解决方案


根据您的逻辑,您的输出中不会有10as freq,因为它只出现一次:

df[df.groupby('freq')['freq'].transform('count').ne(1)] #change to .gt() for greater than 1

    freq id
0    11  a
1    11  b
4     1  e
5     1  f

推荐阅读