2017-08-01 95 views

回答

1

你可以做所有列的两个数据帧的左join,指定indicator=True所以结果保留了_merge列指示如果在A中的行匹配一个选自B,并且相应地更新第二列:

A_ = A.merge(B.drop_duplicates(), indicator=True, how="left") 

# if the row comes from left only then don't make change, otherwise update the second column 
# with first column value 
A_[1] = A_[1].where(A_._merge == "left_only", A_[0]) 
A_.drop('_merge', 1) 

# 0 1 
#0 d e 
#1 d d 
#2 a a 

A = pd.DataFrame([['d','e'],['d','c'],['a','c']]) 
B = pd.DataFrame([['a','c'],['d','c']]) 
+0

它显示我下面: merge()得到了一个意想不到的关键字参数'indicator' – andrew

+0

您使用的是什么熊猫版本?该参数被添加到'0.17.0'上。 – Psidom

+0

看起来A_.drop('_ merge',1)不起作用 – andrew