比较两个不同的行和列的数据帧中的大熊猫
问题描述:
我这里有两个dataframes:比较两个不同的行和列的数据帧中的大熊猫
答:德,直流,交流,
B:AC,DC,
有行不同数量(不等号)。如果A中的一条线与B中的一条线相匹配。我们用A的第一列替换A的第二列。所以结果应该是:
C:德,DD,AA,
答
你可以做所有列的两个数据帧的左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']])
它显示我下面: merge()得到了一个意想不到的关键字参数'indicator' – andrew
您使用的是什么熊猫版本?该参数被添加到'0.17.0'上。 – Psidom
看起来A_.drop('_ merge',1)不起作用 – andrew