使用另一个dataframe和相同的dataframe值之和来更新pandas dataframe

3
我有两个数据框,长这样:
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在df1df2中匹配的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中没有。我希望有人能提供一种替代方案。谢谢!

3个回答

2

首先从df2中创建一个序列,将索引列映射到sales

idx_cols = ['posting_period', 'name']
s = df2.set_index(idx_cols)['sales']

然后使用这个系列更新 df1['sales']

df1['sales'] += pd.Series(df1.set_index(idx_cols).index.map(s.get)).fillna(0)

结果:

print(df1)

   posting_period     name  sales  profit
0               1  client1   60.0    10.0
1               1  client2  100.0    20.0
2               2  client1  170.0    30.0

1
使用左连接的 merge,可以对齐 Series 并进行最后的 add
s = df1.merge(df2, on=['posting_period','name'], how='left')['sales_y']

df1['sales'] = df1['sales'].add(s, fill_value=0)
print (df1)
   posting_period     name  sales  profit
0               1  client1   60.0    10.0
1               1  client2  100.0    20.0
2               2  client1  170.0    30.0

0

你可以使用 pd.concat 以及 sum

pd.concat([df1.set_index(['posting_period', 'name']),df2.set_index(['posting_period', 'name'])],1).sum(level=0,axis=1).reset_index()
Out[728]: 
   posting_period     name  sales  profit
0               1  client1   60.0    10.0
1               1  client2  100.0    20.0
2               2  client1  170.0    30.0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接