Mamed
Mamed

Reputation: 772

Delete rows based on intermediate dataframe

df = 

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

I want to see how many times every value of freq is stored and if recorded one time, remove it. Desired output:

  count = 

freq     recordings
  11      2
  10      1
   9      1
   1      2

and then

df = 

freq     id
  11      a
  11      b
   1      e 
   1      f

Upvotes: 1

Views: 38

Answers (2)

BENY
BENY

Reputation: 323326

IIUC duplicated

df=df[df.freq.duplicated(keep=False)].copy() # add copy for prevent the future copy warning 
   freq id
0    11  a
1    11  b
4     1  e
5     1  f

Upvotes: 2

anky
anky

Reputation: 75100

Per your logic, you sould not have 10 as freq in your output since it appears only once:

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

Upvotes: 3

Related Questions