简体   繁体   English

使用 Python 合并不同文件夹中的 CSV 个文件

[英]Merge CSV files in different folders using Python

I have about 5600 directories structured as follows:我有大约 5600 个目录结构如下:

目录结构

I need to merge all A files into one file, all B files into another file, and so on.我需要将所有 A 文件合并到一个文件中,将所有 B 文件合并到另一个文件中,依此类推。

How can I do this?我怎样才能做到这一点?

IIUC, this should work for your case (I used a RootDir with 2 subdirectories Dir1 and Dir2 with in each 2 files A.csv and B.csv ). IIUC,这应该适用于您的情况(我在每个 2 个文件A.csvB.csv中使用了带有 2 个子目录Dir1Dir2RootDir )。 You can change the value of rootdir to match your usecase:您可以更改rootdir的值以匹配您的用例:

import os
import pandas as pd
rootdir = 'RootDir' # Change when needed to your root directory
files = [os.path.join(dp, f) for dp, dn, filenames in os.walk(rootdir) for f in filenames if os.path.splitext(f)[1] == '.csv']
names = set([x.rstrip('.csv').split('/')[-1] for x in files])
df_dict = {key: pd.DataFrame() for key in names}
for file in files:
    key = file.rstrip('.csv').split('/')[-1]
    df = pd.read_csv(file)
    df_dict[key] = pd.concat([df_dict[key], df])

Output is a dictionary of dataframes df_dict with A and B as keys. df_dict是以AB为键的数据帧 df_dict 的字典。

Use df_dict['A'] to access DataFrame A and so on...使用df_dict['A']访问 DataFrame A等等......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM