我有两个数据框,长这样:
我希望更新
df1
posting_period name sales profit
1 client1 50.00 10.00
1 client2 100.00 20.00
2 client1 150.00 30.00
df2 (this df does not have the 'profit' column as in df1)
posting_period name sales
1 client1 10.00
2 client1 20.00
我希望更新
df1
中客户1的销售额,更新的值为客户1在df1
和df2
中匹配的posting_periods下的销售额之和。换句话说,将客户1在这两个数据框中的销售额相加并更新到df1
中。desired result
posting_period name sales profit
1 client1 60.00 10.00
1 client2 100.00 20.00
2 client1 170.00 30.00
我正在使用的实际数据框要大得多,但这些示例捕获了我所要完成的内容。我想出了一种非常绕弯子的方法,不仅没有起作用,而且也不太符合Python语言风格。另一个挑战是df1
中有一个额外的列,而df2
中没有。我希望有人能提供一种替代方案。谢谢!