Skip to main content
 首页 » 编程设计

python之根据中间数据框删除行

2025年05月04日20grandyang

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 

请您参考如下方法:

根据您的逻辑,您的输出中不应该有 10 作为 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