【问题标题】:Merging two files line by line using Pandas or Python使用 Pandas 或 Python 逐行合并两个文件
【发布时间】:2021-05-11 13:24:53
【问题描述】:

我有两个文件(示例:A.txt 和 B.txt),其中“A.txt”非常大。我想避免将完整文件读入内存,并在合并来自“B.txt”的匹配之前逐行读取。这两个文件也都有标题。

我当前的代码如下所示:

import os
import pandas as pd

contigs=pd.read_csv("A.txt", header=0, sep="\t")
coverages=pd.read_csv("B.txt", header=0, sep="\t")
merged=pd.merge(contigs, coverages, on='contig')
merged.to_csv("merged_file.txt", sep="\t", index=False)

代码有效,但如上所述,我想逐行读取“A.txt”,而不是完全读取内存,并在写出之前与“B.txt”合并。

非常感谢您的帮助!

(使用示例文件更新原始帖子)

head A.txt
clusterID       kegg_contig     contig
Cluster_10700   Unassigned_ERR1801630_792963    ERR1801630_contig_792963
Cluster_10700   Unassigned_ERR1801633_537686    ERR1801633_contig_537686
Cluster_10700   Unassigned_ERR505054_53474      ERR505054_contig_53474
Cluster_10700   Unassigned_ERR505054_31574      ERR505054_contig_31574


head B.txt
contig  coverage
ERR1726751_contig_1     28.82716
ERR1726751_contig_2     12.265934
ERR1726751_contig_3     17.733767

【问题讨论】:

  • 查看块合并 stackoverflow.com/questions/58441517/… file = "tableFile/123456.txt" initDF = pd.read_csv(file, sep="\t", header=0) file2 = "tableFile/7891011.txt " 对于 pd.read_csv(file2, sep="\t", chunksize=50000, header=0) 中的块:initDF = initDF.merge(chunks, how='right', on=['Mod', "Nuc" , "AA"])
  • 感谢@GoldenLion。我试过这个,但不管它在最后打印一个empty 数据框。
  • 你能发一个a和b数据框的样本,我会用数据测试代码
  • 谢谢@GoldenLion。两个文件的前几行都贴在原文中
  • 在数据样本中,contig 字段没有匹配项

标签: python pandas merge


【解决方案1】:
 initDF = pd.read_csv("merge_a.csv", sep=",", header=0) 
 file2 = "merge_b.csv" 
 for chunks in pd.read_csv(file2, sep=",", chunksize=50, header=0): 
      print(chunks)
      initDF = initDF.merge(chunks, how='inner', on=['contig'])

 print(initDF)

【讨论】:

  • 谢谢,但我又有一个empty 数据框。 ``` >>> contigs=pd.read_csv("contigs/Cluster_10700_contigs.txt", sep="\t", header=0) >>> file2="coverage/merged_euci_coverages.txt" >>> 用于 pd 中的块.read_csv(file2, sep="\t", chunksize=50000, header=0): ... contigs=contigs.merge(chunks, how='inner', on=['contig']) ... > >> contigs.head() 空数据帧```
  • 我没有在您提供的数据的 contigs 字段中看到任何匹配项
【解决方案2】:

对于这样一个简单的问题,很容易逐行处理一个文件,只要合并字段在另一个文件中是唯一的。对于答案的其余部分,我将假设 contig 在 B.txt 中是唯一的:

import csv

# load B into a dictionary
with open('B.txt') as file_B:
    rd = csv.reader(file_B, delimiter='\t')
    _ = next(rd)             # skip header line
    dict_B = {row[0]: row[1] for row in rd}

# process file A line by line
with open('A.txt') as fdin, open('merged_file.txt', 'w', newline='') as fdout:
    rd = csv.reader(fdin, delimiter='\t')
    wr = csv.writer(fdout, delimiter='\t')
    # process header line
    row = next(rd)
    row.append('coverage')  # append last column header
    wr.writerow(row)
    # process data line
    for row in rd:
        row.append(dict_B[row[2]])  # append last field
        wr.writerow(row)

这仅使用 csv 模块,不使用 pandas。这将节省大量内存,但可能需要更多时间...

【讨论】:

  • 谢谢,但收到以下错误:``` 26 Traceback(最近一次调用最后一次):文件“”,第 10 行,在 索引错误:列表索引超出范围```
猜你喜欢
  • 2019-02-22
  • 2016-07-30
  • 1970-01-01
  • 2013-10-03
  • 2013-04-29
  • 1970-01-01
  • 1970-01-01
  • 2017-07-03
  • 1970-01-01
相关资源
最近更新 更多