merge two files based on common column values

梦想的初衷 提交于 2020-05-09 11:38:31

问题


I have file1 likes:

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

And a file2:

1 A 11
2 B 22
3 C 33

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.


回答1:


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

and df2:

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

then you can use merge:

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

to get

   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



回答2:


Which way is the simplest

I am not sure what do you mean by simplest. For this problem, you can simply use join:

join -j 2 -o 1.1 1.2 1.3 2.3 file1 file2

For the given example, the above command generates the desired output. If your file is not sorted, you can also add --nocheck-order option.



来源:https://stackoverflow.com/questions/52508710/merge-two-files-based-on-common-column-values

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!