【发布时间】:2015-08-25 09:45:33
【问题描述】:
我需要一个包含多个列的单个文件(=目录中的文件数),来自目录中的多个文件。每个文件都有唯一的 ID,所有文件都不会更改,因此我需要基于合并这些文件在那个 id 上。
例如, file_1 看起来像这样
id pool1
ABL1 1352
ABL12 1236
ABL13 1022
ABL14 815
ABL15 1591
ABL16 2703
因此与其他文件一样,目录中所有其他文件的第一列相同,第二列不同。
我正在寻找类似这样的输出,
id /pool1 /pool2 /pool3 /pool4 /pool5
ABL1 1352 1353 1354 1355 1356
ABL12 1236 1237 1238 1239 1240
ABL13 1022 1023 1024 1025 1026
ABL14 815 816 817 818 819
ABL15 1591 1592 1593 1594 1595
ABL16 2703 2704 2705 2706 2707
ABL17 1449 1450 1451 1452 1453
ABL18 619 620 621 622 623
ABL19 1074 1075 1076 1077 1078
到目前为止,我试图通过以下脚本在 python 中实现它,
path = '/Pool1'
files = os.listdir(path)
files_txt = [i for i in files if i.endswith('.txt_samplecount')]
files_merge= i for i in files_txt if i.merge(i,on="id")
But it throws error as
AttributeError: 'str' object has no attribute 'merge'
欢迎任何帮助或建议
谢谢
【问题讨论】:
-
files_merge的元素是字符串——文件名。字符串类型没有merge()方法。您将需要编写一些从文件中读取的代码,例如创建将第一列映射到第二列的字典,然后合并字典 -
好的,我明白了,现在它可以帮助我使用以下代码,dfs = [pd.DataFrame.from_csv(x, sep='\t') for x in files_txt] merge = pd.concat (dfs, 轴=1)
-
抱歉,难以辨认
-
不行,它可以给我我需要的输出..!!!