【问题标题】:merge in python by certain columns from list of files通过文件列表中的某些列在 python 中合并
【发布时间】:2020-08-04 20:58:09
【问题描述】:

我有一个目录,其中包含 >200 个制表符分隔的文件,所有文件的结构都相同(列 #s,列标题)

+------+--------------+-------+-------+-----------+------+---------+
| col1 |     col2     | col2  | col3  | p_val_adj | col4 |  gene   |
+------+--------------+-------+-------+-----------+------+---------+
|    0 |  1.980029448 | 0.978 | 0.124 |         0 |    0 | TRDV2   |
|    0 |  1.812616859 | 0.979 | 0.176 |         0 |    0 | TRGV9   |
|    0 |  1.442023797 | 0.688 |  0.09 |         0 |    0 | TRDC    |
|    0 | -1.834847304 | 0.021 | 0.735 |         0 |    0 | TRAV1-2 |
+------+--------------+-------+-------+-----------+------+---------+

我的目标是生成一个包含“基因”作为第一列的输出文件,并合并所有文件中的所有“avg_logFC”列数据。对于不重叠的基因,将该值留空。

要完成的步骤: 1) 读取以 .txt [done] 结尾的目录中的所有文件 ex: File1.txt , File2.txt, File3.txt 2)使用列“基因”作为index_col [完成] 3)将文件合并到一个数据框[错误] 4) 列标题而不是 avg_logFC 应该反映文件名

这是我目前所做的:

args = parse_args()
    path=os.getcwd() #opens the path
    allFiles = glob.glob(path + "/*.txt")    #reads all files in the path with .txt
    result = pd.read_csv(allFiles[0], sep="\t", index_col=["gene"]) #index by gene
    for i in range(1,len(allFiles)): #iterates over remaining files; note first file is 0.
        print i
        df = pd.read_csv(allFiles[i], sep="\t", index_col=["gene"])
        result = pd.merge(result, df, right_index=False, left_index=True, how='inner')
result.to_csv(args.output+".xls", sep="\t", na_rep="")

我无法获得所需的输出,如下所示。

+----------+-------+-------+--------+
|  genes   | File1 | File2 | File 3 |
+----------+-------+-------+--------+
| TRDV2    |   0.5 |    12 |      2 |
| TRGV9    |     2 |     2 |        |
| TRDC     |    -2 |     3 |      1 |
| TRAV1-2  |       |    21 |     -5 |
| CD8A     |  0.24 |       |     -2 |
| TRBV20-1 |     3 |     1 |     -2 |
| TRBC1    |   0.2 |       |      3 |
| FCGR3A   |     1 |     2 |      4 |
+----------+-------+-------+--------+

【问题讨论】:

    标签: pandas select merge col


    【解决方案1】:

    第一件事是 how 应该是 outer 而不是 inner 否则你只会得到所有数据框中的基因,我也不确定为什么 right_index 如果你想要的话是 False合并在索引的基因上。不清楚的第二件事是 avg_logFC 列在哪里,因为它不在您的示例中,我假设它是一个列,如果是这样,您不应该 merge 完整的 df 而是在 @ 之前只选择此列987654327@。最后,在每次迭代中使用merge 并不是很有效(不确定这里是否有问题)。我认为在这里使用concat 会更好:

    args = parse_args()
    path=os.getcwd() #opens the path
    allFiles = glob.glob(path + "/*.txt")    #reads all files in the path with .txt
    
    result = pd.concat([pd.read_csv(file, sep="\t", index_col=["gene"], usecols=['avg_logFC'])
                        for file in allFiles], 
                       axis=1, keys=range(len(allFiles)))\
               .reset_index()
    
    result.to_csv(args.output+".xls", sep="\t", na_rep="")
    

    不确定确切的输出,但它应该与您要查找的内容非常接近。也许你需要renamereset_index之前的列

    【讨论】:

    • 我试过了,它给出的语法无效。不确定这是否有效。
    • @BioProgram 如果您详细说明失败的地方,我可以帮助您。我还意识到在to_csv中,文件的扩展名是".xls",而应该是".csv"
    猜你喜欢
    • 2021-12-07
    • 2020-02-10
    • 1970-01-01
    • 2013-08-01
    • 2010-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多