How to merge data with same data in pandas?

Question:

i have this following data:

Date_reported Country_code Country WHO_region New_cases Cumulative_cases New_deaths Cumulative_deaths
0   2020-01-03  AF  Afghanistan EMRO    0   0   0   0
1   2020-01-04  AF  Afghanistan EMRO    0   0   0   0
2   2020-01-05  AF  Afghanistan EMRO    0   0   0   0
3   2020-01-06  AF  Afghanistan EMRO    0   0   0   0
4   2020-01-07  AF  Afghanistan EMRO    0   0   0   0
... ... ... ... ... ... ... ... ...
243868  2022-10-23  ZW  Zimbabwe    AFRO    0   257893  0   5606
243869  2022-10-24  ZW  Zimbabwe    AFRO    0   257893  0   5606
243870  2022-10-25  ZW  Zimbabwe    AFRO    0   257893  0   5606
243871  2022-10-26  ZW  Zimbabwe    AFRO    0   257893  0   5606
243872  2022-10-27  ZW  Zimbabwe    AFRO    0   257893  0   5606

i want to merge the all the country into a table like:

Date_reported New_cases Cumulative_cases New_deaths Cumulative_deaths
0   2020-01-03  0   0   0   0
1   2020-01-04  0   0   0   0
2   2020-01-05  0   0   0   0
3   2020-01-06  0   0   0   0
4   2020-01-07  0   0   0   0
... ... ... ... ... ... ... ... ...
243872  2022-10-27  sum  of  all    country

how can i use pandas to do it?

Asked By: finko

||

Answers:

You want to group the data by the date column and then sum each column:

df.groupby(['Date_reported'])['New_cases', 'Cumulative_cases', 'New_deaths', 'Cumulative_deaths'].sum()
Answered By: richardjmorton
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.