我想在python中合并一个 Dataframe
df 1
Product_Type Product_Colour Notes
0 Shirt Black 2 L and 2 S
1 Shirt Black 1 XL and 2 M
2 Shirt Black 2 XS and 2 S
df2
Product_Type Product_Colour Code
13 Shirt Black KI
14 Shirt Black KI
25 Shirt Black KI
......
that df2 still have many rows. I just want to , if both Product type and colour appears in both df, take the code information and make df3
this is expected result,
Product_Type Product_Colour Notes Code
0 Shirt Black 2 L and 2 S KI
1 Shirt Black 1 XL and 2 M KI
2 Shirt Black 2 XS and 2 S KI
but i got this instead
Product_Type Product_Colour Notes Code
0 Shirt Black 2 L and 2 S KI
字符串
我用pd.merge(df1,df2 on=['Product_Type','Product_Colour'], how='left')
做的,它似乎不能处理产品类型和产品颜色的非唯一值。
2条答案
按热度按时间zengzsys1#
由于您对唯一值感兴趣,因此可以使用
drop_duplicates
函数在合并之前使df2
不同。字符串
完整的解决方案,考虑到列:
型
np8igboo2#
字符串
输出量:
型