I have a data frame with 101 columns currently. The first column is called "Country/Region" and the other 100 are dates in MM/DD/YY format from 1/22/20 to 4/30/20 like the example below. I would like to combine repeat country entries such as 'Australia' below and have its values in the date columns to be added together so that there is one row per country. I would like to keep ALL date columns.I have tried to use the groupby() and agg() functions but I do not know how to sum() together that many columns without calling every single one. Is there a way to do this without calling all 100 columns individually?
Country/Region | 1/22/20 | 1/23/20 | ... | 4/29/20 | 4/30/20
Afghanistan 0 0 ... 1092 1176
Australia 0 0 10526 12065
Australia 0 0 ... 56289 4523
CodePudding user response:
This should work:
df.pivot_table(index='Country/Region', aggfunc='sum')
CodePudding user response:
Did you already try this? It should also give the expected result.
df.groupby('Country/Region').sum()
CodePudding user response:
You can do this:
df.iloc[:,1:].sum(axis=1)