我想像在Excel中那样在熊猫中进行vlookup。换句话说:
我这样做了:vlookup = df1.merge(df2, on = 'A')
,但它复制了B的列值并将其放置在各处。
df1 =
A B
car VB
car K2
car V9
car VX
df2 =
A C
car VB
car K2
cat BN
monkey AS
所需的输出:
df3 =
A D
car VB
car K2
cat NaN
car NaN
cat NaN
答案 0 :(得分:1)
您应该
df3=df1.merge(df2.rename(columns={'C':'B'}),on=['A','B']).rename(columns={'B':'D'})
A D
0 car VB
1 car K2