Using transform to filter dataframe based on groupby information

Question:

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]
Asked By: Henrik

||

Answers:

Specify column after groupby:

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

Alternative:

df = data[data['id'].map(data['id'].value_counts())==3]
Answered By: jezrael
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.