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?
>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.
