Home > Software design >  Using transform to filter dataframe based on groupby information
Using transform to filter dataframe based on groupby information

Time:10-20

I want to filter out id's that not appear 3 times in the dataset below. I thought of using groupby and transform('size'), but that doesn't work.

Why?

data = pd.DataFrame({'id':[0,0,0, 1,1,1, 2,2, 3,3,3, 4, 4],
              'info':[23,22,12,12,14,23,11,2,98,76,46,341,12]})

data[data.groupby(['id']).transform('size')==3]

CodePudding user response:

Specify column after groupby:

df = data[data.groupby(['id'])['id'].transform('size')==3]

Alternative:

df = data[data['id'].map(data['id'].value_counts())==3]
  • Related