【问题标题】:Python; write dataframe output to different subdirectoriesPython;将数据帧输出写入不同的子目录
【发布时间】:2017-05-03 19:51:50
【问题描述】:

我正在从我当前的工作目录运行我的脚本。使用我的脚本,我遍历当前工作目录的子目录。每个子目录包含脚本中提到的 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')

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    试试这个:

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

    PS 确保这个命令在for loop

    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 会给出未定义 df1 的错误。它在行中给出了错误:dfmerge1 = pd.merge(df1, df2, on=['genome', 'contig'], how='outer')
    【解决方案2】:

    只需添加to_csv()的路径

    df_end.to_csv('your/path/here/outputgenesdf.csv')
    

    【讨论】:

    • 我当前的工作目录包含 100 多个子目录,每个子目录包含 3 个要合并到数据框中的文件。我无法指定子目录的路径,因为它们都有不同的名称。所以我必须在循环中的某处添加命令 df.to_csv ,以将其保存在所有子目录中,但我不知道如何......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-01
    • 2020-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-21
    • 2018-01-31
    相关资源
    最近更新 更多