這個問題在這里已經有了答案: 我如何熊貓分組獲得總和? (10 個回答) 1 小時前關閉。
我有以下資料:
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
我想將所有國家合并到一個表格中,例如:
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
我如何使用熊貓來做到這一點?
uj5u.com熱心網友回復:
您想按日期列對資料進行分組,然后對每一列求和:
df.groupby(['Date_reported'])['New_cases', 'Cumulative_cases', 'New_deaths', 'Cumulative_deaths'].sum()
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/522197.html
標籤:Python熊猫日期合并
