根据共同的列值合并两个文件 [英] merge two files based on common column values

查看:42
本文介绍了根据共同的列值合并两个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有file1个赞:

1 A aa
2 A bb
3 A cc
4 A dd
5 B xx
6 C yy
7 C zz

还有一个文件2:

1 A 11
2 B 22
3 C 33

我想将file1和file 2合并到基于第二列的file3中,这样:

And I would like to merge file1 and file 2 into a file3 based on the 2nd column, such that:

1 A aa 11
2 A bb 11
3 A cc 11
4 A dd 11
5 B xx 22
6 C yy 33
7 C zz 33

哪种方法最简单?谢谢.

Which way is the simplest? Thank you.

推荐答案

使用 pandas 将为您节省如果您使用Python,则会花费很多时间.因此,如果您的DataFrame是df1:

Using pandas will save you a lot of time if you use Python. So if your DataFrames are df1:

   1   2
0
1  A  aa
2  A  bb
3  A  cc
4  A  dd
5  B  xx
6  C  yy
7  C  zz

df2:

   1   2
0
1  A  11
2  B  22
3  C  33

然后您可以使用 merge :

then you can use merge:

df1.merge(df2, left_on=1, right_on=1)

获得

   1 2_x  2_y
0  A  aa   11
1  A  bb   11
2  A  cc   11
3  A  dd   11
4  B  xx   22
5  C  yy   33
6  C  zz   33

这篇关于根据共同的列值合并两个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆