简体   繁体   English

从不同子文件夹的文件中读取数据帧

[英]Read dataframes from files of different subfolders

I have 12 different directories, looking something like that: 我有12个不同的目录,看起来像这样:

directories = ['FOLDER/subfolder01/*.csv', 'FOLDER/subfolder02/*csv', ... ]

12 subfolders within one major FOLDER, each one containing a set of csv files with the same data format. 一个主要文件夹中的12个子文件夹,每个子文件夹包含一组具有相同数据格式的csv文件。

I'd like to loop over it and somehow read the dataframes within each subfolder and then continue with the plots. 我想遍历它,以某种方式读取每个子文件夹中的数据框,然后继续进行绘制。

Is there a way to set the subfolders as indexes over which I can manipulate with the files? 有没有一种方法可以将子文件夹设置为可以使用文件操作的索引?

from glob import glob
directory_dfs = {directory_name: load_csvs_from_directory(directory_name)
                 for directory_name in glob('FOLDER/subfolder*/')}

Then just encapsulate your logic for how to read all the CSV's from a directory into the load_csvs_from_directory function, and voila, you have a labelled collection of the DataFrames built from the data in each directory. 然后,只需封装有关如何从目录中读取所有CSV的逻辑到load_csvs_from_directory函数中,瞧,您就有从每个目录中的数据构建的带标签的DataFrame集合。

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

相关问题 使用 Python 搜索(在文件夹和子文件夹中)并将文件读取到数据帧列表 - Search (in folders and subfolders ) and read files to a list of dataframes, using Python 阅读文件夹中的所有xlsx文件,并将文件保存在不同的DataFrame中 - Read all xlsx-Files in a folder and save the files in different DataFrames 使用循环读取和保存不同数据帧下的不同文件 - use a loop to read and save different files under different dataframes 如何读取子文件夹中的 json 文件? - How to read json files in subfolders? 将来自不同子文件夹的同名文件 csv 合并为一个 csv - Combine csv files with same name from different subfolders in to one csv 试图将文件从许多不同的子文件夹移动到主子文件夹 - trying to move files from many different subfolders to a main subfolder 从子文件夹中提取文件 - Extracting files from subfolders 如何将源文件中的 excel 文件动态读取到 python 中的不同数据帧中 - How to read excel files dynamically in a source file into different dataframes in python python 读取具有相同基本名称的 csv 文件并保存为不同的数据帧 - python read csv files with same basename and save as different dataframes 如何读取不同 Pandas Dataframes 中的多个 excel 文件 - How to read multiple excel files in Different Pandas Dataframes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM