yasin mohammed
yasin mohammed

Reputation: 471

Aggregating a dataframe to give the sum of values

I tried to group data based on multiple fields but the groupby is not coming in a perfect dataframe

Comgroup Cd Geo IMT Best Remit Name Q1  Q2  Q3  Q4  Total
IT Product  AP  ANZ AVNET   0   0   0   823.09  823.09
IT Product  AG  ANZ TOSHIBA 0   0   5065.4  237060.72   242126.12
IT Product  EMEA    ANZ LENOVO  126664.47   0   0   113285.78   239950.25
IT Product  AP  ANZ LENOVO  196154.85   0   1122.15 10252.13    207529.13

I wrote the code as below and was the grouping has come in a totally different pattern.

f = {'Q1':['sum'] , 'Q2':['sum'] , 'Q3':['sum'] , 'Q4':['sum'], 'Total':['sum']} 
total_spendfinal = total_spendfinal.groupby(['Geo','IMT','Spend Type','Spend List']).agg(f)

Image of how the dataframe looks like is attached.

Upvotes: 0

Views: 45

Answers (1)

A.Kot
A.Kot

Reputation: 7903

Need to do total_spendfinal.reset_index(inplace=True)

Upvotes: 1

Related Questions