Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using transform to filter dataframe based on groupby information

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]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Specify column after groupby:

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

Alternative:

df = data[data['id'].map(data['id'].value_counts())==3]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading