【发布时间】:2021-06-04 14:00:01
【问题描述】:
我正在尝试将两个数据框相互组合。但我被逻辑困住了(也许我对这个问题看得太久了)。
问题: DF1 的格式如下:
Country ID March April May June
Netherlands A 10 20 15 14
Netherlands B 15 18 10 4
Germany C 9 8 3 1
然后,我有另一个数据框:
DF2
Country ID Month Number
Netherlands A March 8
Netherlands A March 28
Netherlands B April 18
Germany C Jan 9
我想合并这两列(格式与 DF1 相同)。 我的假设是应该是这样的,按(国家+ ID)分组,然后(以月份为列,对数字求和)。 - 但如前所述,我很困惑。
期望的输出:
Country ID March April May June df2_Jan df2_March df2_April
Netherlands A 10 20 15 14 0 36 #8+28 0
Netherlands B 15 18 10 4 0 0 18
Germany C 9 8 3 1 9 0 0
希望你能帮助我。提前非常感谢!
【问题讨论】:
-
你看过这个吗:geeksforgeeks.org/…
标签: python pandas data-wrangling