我有两个数据帧:
A:
foo bar foo2
0 1 1 1
1 2 2 2
2 3 3 3
3 3 3 3
B:
foo
0 1
2 2
3 1
4 5
我想将A中索引存在的B的值加到A的列中(在具有相应索引的行上)WITHOUT 来自B的值,在A中没有相应的索引,或者删除A和B中都不存在的索引:
foo bar foo2
0 2 1 1
1 2 2 2
2 5 3 3
3 6 3 3
我觉得这应该是直截了当的但是使用add
和concat
我最终得到的不是A中的所有行或A和B的联合
答案 0 :(得分:1)
将add
与fill_value=0
一起使用,最后还需要dropna
A.add(B,fill_value=0).dropna().astype(int)
Out[434]:
bar foo foo2
0 1 2 1
1 2 2 2
2 3 5 3
3 3 4 3
答案 1 :(得分:1)
简单,直接,高效 - 使用集合操作来获取索引的交集,然后执行基于loc
的算术 -
i = A.index.intersection(B.index)
j = A.columns.intersection(B.columns)
A.loc[i, j] += B.loc[i, j]
A
foo bar foo2
0 2 1 1
1 2 2 2
2 5 3 3
3 4 3 3
答案 2 :(得分:0)
你可以做到
/s
答案 3 :(得分:0)
另一个选项可能是:
Sub Send_Selection_Or_ActiveSheet_with_MailEnvelope()
Dim Sendrng As Range
On Error GoTo StopMacro
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set Sendrng = Selection
With Sendrng
ActiveWorkbook.EnvelopeVisible = True
With .Parent.MailEnvelope
.Introduction = " "
With .Item
.To = "adicker@generic.com"
.CC = ""
.BCC = ""
.Subject = "WC Referral Notice"
.Send
End With
End With
End With
StopMacro:
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
ActiveWorkbook.EnvelopeVisible = False
End Sub
结果:
result_df = (A + B).fillna(A).dropna()
print(result_df)