在一个大学项目中,我使用的是约翰霍普金斯冠状病毒新冠肺炎的数据集:https://github.com/CSSEGISandData/COVID-19。我尝试的是让数据集变得更简单。这是我现在的数据集:
Country Date Confirmed Deaths Recovered
2600 Mainland China 2020-02-28 410.0 7.0 257.0
2601 Iran 2020-02-28 388.0 34.0 73.0
2602 Mainland China 2020-02-28 337.0 3.0 279.0
2603 Mainland China 2020-02-28 318.0 6.0 277.0
2604 Mainland China 2020-02-28 296.0 1.0 235.0
... ... ... ... ... ...
2695 US 2020-02-25 1.0 0.0 1.0
2696 US 2020-02-24 0.0 0.0 0.0
2697 US 2020-02-24 0.0 0.0 0.0
2698 US 2020-02-24 0.0 0.0 0.0
2699 Mainland China 2020-02-29 66337.0 2727.0 28993.0如果Country和Date列中的值相同,我希望汇总所有已确认、死亡和恢复的值。
例如,在第2600、2602、2603、2604行中,Country和Date列中的值匹配,因此我希望组合这些行,并分别汇总确认、死亡和恢复列。它应该给出下面的行:
2600 Mainland China 2020-02-28 1361.0 17.0 1048.0到目前为止,我所拥有的:
duplicateRowsDF = df[df.duplicated(['Country', 'Date'])]
duplicateRowsDF希望有人能帮助我,最好是熊猫,但不限于熊猫。提前谢谢。
发布于 2020-03-07 22:07:10
使用groupby怎么样?如果您这样做:
df.groupby(by=['Country', 'Date']).sum() 具有相同国家/地区和日期的所有行将仅分组到一列中,每列中的所有值的总和。
https://stackoverflow.com/questions/60578312
复制相似问题