Home > Software design >  Aggregated sum on date in pandas
Aggregated sum on date in pandas

Time:02-23

My df looks like this

Date Col Col1
01/01/2022 A 500
01/01/2022 B 100
01/01/2022 C 400
02/01/2022 A 400
02/01/2022 B 150
02/01/2022 C 450

My desired output looks like

Date Total
01/01/2022 1000
02/01/2022 1000

Please help. I wanna do it automatically (not manually-hardcoded)

I am trying this

df.groupby('Date')['Col1'].sum()

CodePudding user response:

Try just summing the entire group, rather than a specific column:

df.groupby('Date').sum()

CodePudding user response:

If you need totals and the separate column values for a given date, follow this general format.

needed_columnms = ['List','Of','Needed','Columns']
df_sums = df.groupby('Date')[needed_columns].sum()
df_sums['Total'] = df_sums[needed_columns].sum(1)

df_sums will provide you with a column total and grand total for each of the dates within 'Date'.

  • Related