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

How to filter a groupby sum

I am looking to total sections of a total column using the groupby function. When I use the groupby function ‘code’ it works however I would like to be able to filter it down to one nominal code by placing it in a variable and printing it.

subheading_one = df.groupby(['Code'])['Total'].sum()
subheading_two = df.groupby(['Code'])['Total'].sum()
subheading_three = df.groupby(['Code'])['Total'].sum()

print('Cost heading 1.1 £: ',subheading_one)
print('Cost heading 1.2 £: 'subheading_two)
print('Cost heading 1.3 £: 'subheading_three)

I have attached a snippet of the data frame. As you can see I would like to be able to total only ‘1.1’ items and place that total into a variable (should equal 300). Can anybody help?

enter image description here

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 can use the get_group like this

subheading_one = df.groupby(['Code'])['Total'].get_group('1.1').sum()

I am assuming your Code feature has string values.

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