我想从多个txt文件创建一个数据帧,但是在一个数据帧中合并一个数据帧列表时遇到关键错误。
我尝试在合并之前重置数据帧的索引,但没有帮助。我在Jupyter Python 3上运行。每个txt文件都有相同的列(“ Geneid”,“ Chr”,“ Start”,“ End”,“ Strand”,“ Length”)和示例的1列(“样本1”等),最后我想在通用列(“ Geneid”,“ Chr”,“ Start”,“ End”,“ Strand”,“ Length”)上合并一个df并排列出样本1,2,3等,如果缺少值则可能用“ 0”显示
#list the files
filenames = os.listdir('path')
# Append every single file to a distinct dataframe
list_dataframes = []
for filename in filenames:
list_dataframes.append(pd.read_table(os.path.join('path', filename)))
for dataframe in list_dataframes:
dataframe.reset_index()
print(dataframe.columns.tolist())
#results of print: ['Geneid', 'Chr', 'Start', 'End', 'Strand', 'Length', 'sample1']
# importing functools for reduce()
import functools
from functools import reduce
#Merge dataframes in one single df
df=reduce(lambda left, right: pd.merge(left, right, on = ['Geneid', 'Chr','Start','End','Strand','Length'], how='outer'), list_dataframes)
预期:一个单独的df,其中“示例列”根据通用列(“ Geneid”,“ Chr”,“ Start”,“ End”,“ Strand”,“ Length”)合并。
错误:KeyError:'Geneid'