汇总表中特定列上具有相同值的行

2024-06-16 14:50:24 发布

您现在位置:Python中文网/ 问答频道 /正文

对于一个大学项目,我使用约翰·霍普金斯冠状病毒新冠病毒-19数据集: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

希望有人能帮我,最好是熊猫,但不限于熊猫。提前谢谢


Tags: 数据项目httpsgithubcomdfdatecountry