简体   繁体   English

根据共同的列值合并两个文件

[英]merge two files based on common column values

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: 和一个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: 我想基于第二列将file1和file 2合并到file3中,这样:

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. 谢谢。

Using pandas will save you a lot of time if you use Python. 如果使用Python,使用熊猫会节省很多时间。 So if your DataFrames are df1 : 因此,如果您的DataFrame是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 : df2

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

then you can use merge : 然后您可以使用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

这是awk的解决方案。

awk 'FNR==NR{a[$2]=$3;next} {print $0,a[$2]}' Input_file2  Input_file1

Which way is the simplest 哪种方法最简单

I am not sure what do you mean by simplest . 我不确定最简单的意思是什么。 For this problem, you can simply use join : 对于这个问题,您可以简单地使用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. 如果您的文件未排序,则还可以添加--nocheck-order选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM