有人有更好的方法可以有效地从60000个txt文件中创建一个DataFrame,其中一列是键,第二列是值



免责声明这是我的第一篇帖子,如果我没有达到社区的某些标准,我很抱歉
_____________________________________________________________________________________


我使用python3,Jupyter笔记本,Pandas

我使用KMC kmer计数器在合理的时间内对60000个DNA序列的kmer进行计数。我想使用这些kmer计数作为ML算法的输入,作为单词袋模型的一部分

包含kmer计数的文件的形状如下,或者如图所示,我有60K文件:

AAAAA C2


我想从所有60K文件中创建一个DataFrame,每个DNA序列有一行kmer计数,其形式如下:目标DataFrame形状

第一种方法是成功的,我成功地在58秒内导入了100个序列(100个txt文件(,使用以下代码:

import time
countsPath = r'D:DataSetMULTIbow6mer'
start = time.time()
for i in range(0, 60000):
sample = pd.read_fwf(countsPath + r'kmers-' + str(k) +'-seqNb-'+ str(i) + '.txt',sep=" ", header=None).T
new_header = sample.iloc[0] #grab the first row for the header
sample = sample[1:] #take the data less the header row
sample.columns = new_header #set the header row as the df header

df= df.append(sample, ignore_index=True)  #APPEND Sample to df DataSet

end = time.time()
# total time taken
print(f"Runtime of the program is {end - start} secs")

#     display(sample)
display(df)

然而,这非常缓慢,在100个文件上花费了59秒。在整个数据集上,取一个系数x600
我尝试了daskDataFrames-Bag来加速这个过程,因为它读取类似字典的数据,但我无法将每个文件附加为一行。生成的Dask DataFrame如下或如图所示:

0 AAAAA 18
1 AAAAC 16
2 AAAAG 13


1023 TTTTT 14
0 AAAAA 5
1 AAAAC 4

1023 TTTTT 9
0 AAAAA 18
1 AAAAC 16
2 AAAAG 13
3 AAAAT 12
4 AAACA 11

因此,这些文件被插入到一列中。

有谁有更好的方法可以从60k txt文件高效创建DataFrame

喜欢免责声明。我也有一个类似的问题——这是我第一次尝试回答问题。但我很确定我明白了。。。你也是:

dict_name=dict(zip(df['column_name],df['the_other_column_name]((

相关内容

最新更新