vlookup使用熊猫而无需重复
我有两个DataFrames DF1和DF2。如何使用PD.Merge或任何其他功能获得DF3?
我尝试了什么?
df3 = df1.merge(df2,on ='a',how ='left')
这给了我df3,其行数与df2相同。但是,我想要的是DF1和DF3中的行数是相同的。 DF3应与提供的示例完全相同。
DF1
A B
1 1
2 1
3 2
4 2
5 3
6 7
7 1
8 1
9 9
10 3
DF2
A C
1 XY
1 XY
2 XY
2 XY
2 XY
3 XY
3 XY
4 XY
4 XY
5 XY
5 XY
6 AB
6 AB
7 AB
8 AB
9 AB
9 AB
9 AB
10 MN
10 MN
DF3
A B C
1 1 XY
2 1 XY
3 2 XY
4 2 XY
5 3 XY
6 7 AB
7 1 AB
8 1 AB
9 9 AB
10 3 MN
I have two dataframes df1 and df2. How do I obtain df3 using pd.merge or any other function?
What I tried?
df3=df1.merge(df2, on='A', how='left')
This gives me df3 with number of rows same as df2. But, what I want is number of rows in df1 and df3 are same. df3 should look exactly as in the example provided.
df1
A B
1 1
2 1
3 2
4 2
5 3
6 7
7 1
8 1
9 9
10 3
df2
A C
1 XY
1 XY
2 XY
2 XY
2 XY
3 XY
3 XY
4 XY
4 XY
5 XY
5 XY
6 AB
6 AB
7 AB
8 AB
9 AB
9 AB
9 AB
10 MN
10 MN
df3
A B C
1 1 XY
2 1 XY
3 2 XY
4 2 XY
5 3 XY
6 7 AB
7 1 AB
8 1 AB
9 9 AB
10 3 MN
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
drop_duplicates
保留一个实例(a,b)
:Use
drop_duplicates
to keep one instance of(A, B)
:地图也工作
map works too