[英]Merge CSV files in different folders using Python
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.csv
和B.csv
中使用了带有 2 个子目录Dir1
和Dir2
的RootDir
)。 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
是以A
和B
为键的数据帧 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.