通过commom列名称合并多个文件

时间:2019-11-16 21:55:45

标签: python pandas

我有多个用空格分隔的文件,这些文件的列数不同。我想加入他们并保留常用的列名。

test1_file

id  SNP1  SNP2 SNP3

162  AA    BB   AA

172  AB    BB   AB

test2_file

id  SNP1  SNP2 SNP3  SNP4

189  BB    BB   AA    AB

1590 AA    AB   BB    AA

test3_file

id  SNP8  SNP2 SNP3  SNP9

182  AA    BB   AA    AA

132  BB    BB   AA    BB

所需的输出文件:

id   SNP2  SNP3

162  BB     AA

172  BB     AB

189  BB     AA

1590 AB     BB

182  BB     AA

132  BB     AA

我的代码:它不起作用,我想添加的文件不止两个。

import pandas as pd

test1_file = pd.read_csv('2014_07_03', delim_whitespace = True)

test2_file = pd.read_csv('2019_08_19', delim_whitespace = True)

f = pd.merge(left=test1_file, right=test2_file , how='left', on='MergeCol')

f.to_csv('outfile.csv')

0 个答案:

没有答案