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

Can I use a specific element inside the groupby functions instead of using column name?

Im trying to use groupby function, is there a way to use a specific element rather than the column name. Example of code.

df.groupby(['Month', 'Place'])['Number'].sum()

This is what I want to do.

df.groupby(['April', Place'])['Number'].sum()

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 :

you need filter your DataFrame before:

df.loc[df['Month'].eq('April')].groupby('Place')['Number'].sum()
#df[df['Month'].eq('April')].groupby('Place')['Number'].sum()
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