gpt4 book ai didi

Python;将数据帧输出写入不同的子目录

转载 作者:行者123 更新时间:2023-12-01 03:01:39 24 4
gpt4 key购买 nike

我正在从当前工作目录运行脚本。通过我的脚本,我循环遍历当前工作目录的子目录。每个子目录都包含脚本中提到的 3 个文件,对于每个子目录,我将这 3 个文件合并到一个数据帧。正如我现在的脚本所示,它仅将一个子目录的合并数据帧写入当前工作目录。我想要的是 csv 文件,其中每个子目录的合并数据帧保存在该子目录中,或者每个子目录的数据帧连接到一个大输出文件的文件。在我的脚本中,输出文件中只有一个子目录的输出。

我的脚本如下:

print('Start merging contig files')

for root, dirs, files in os.walk(os.getcwd()):
filepath = os.path.join(root, 'genes.faa.genespercontig.csv')
if os.path.isfile(filepath):
with open(filepath, 'r') as f1:
df1 = pd.read_csv(f1, header=None, delim_whitespace=True, names = ["contig", "genes"])
df1['genome'] = os.path.basename(os.path.dirname(filepath))

filepath = os.path.join(root, 'hmmer.analyze.txt.results.txt')
if os.path.isfile(filepath):
with open(filepath, 'r') as f2:
df2 = pd.read_csv(f2, header=None, delim_whitespace=True, names = ["contig", "SCM"])
df2['genome'] = os.path.basename(os.path.dirname(filepath))

filepath = os.path.join(root, 'genes.fna.output_blastplasmiddb.out.count_plasmiddbhit.out')
if os.path.isfile(filepath):
with open(filepath, 'r') as f3:
df3 = pd.read_csv(f3, header=None, delim_whitespace=True, names = ["contig", "plasmid_genes"])
df3['genome'] = os.path.basename(os.path.dirname(filepath))

#merge dataframes
dfmerge1 = pd.merge(df1, df2, on=['genome', 'contig'], how='outer')
df_end = pd.merge(dfmerge1, df3, on=['genome', 'contig'], how='outer')

df_end.to_csv('outputgenesdf.csv')

最佳答案

试试这个:

df_end.to_csv(os.path.join(root, 'outputgenesdf.csv'))

PS 确保该命令位于 for 循环

print('Start merging contig files')

for root, dirs, files in os.walk(os.getcwd()):
filepath = os.path.join(root, 'genes.faa.genespercontig.csv')
if os.path.isfile(filepath):
with open(filepath, 'r') as f1:
df1 = pd.read_csv(f1, header=None, delim_whitespace=True, names = ["contig", "genes"])
df1['genome'] = os.path.basename(os.path.dirname(filepath))

filepath = os.path.join(root, 'hmmer.analyze.txt.results.txt')
if os.path.isfile(filepath):
with open(filepath, 'r') as f2:
df2 = pd.read_csv(f2, header=None, delim_whitespace=True, names = ["contig", "SCM"])
df2['genome'] = os.path.basename(os.path.dirname(filepath))

filepath = os.path.join(root, 'genes.fna.output_blastplasmiddb.out.count_plasmiddbhit.out')
if os.path.isfile(filepath):
with open(filepath, 'r') as f3:
df3 = pd.read_csv(f3, header=None, delim_whitespace=True, names = ["contig", "plasmid_genes"])
df3['genome'] = os.path.basename(os.path.dirname(filepath))

#merge dataframes
dfmerge1 = pd.merge(df1, df2, on=['genome', 'contig'], how='outer')
df_end = pd.merge(dfmerge1, df3, on=['genome', 'contig'], how='outer')

df_end.to_csv(os.path.join(root, 'outputgenesdf.csv'))

关于Python;将数据帧输出写入不同的子目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43768825/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com