[英]how to create a dict of dict of dict of dataframes from list of file paths?
I have a list of file paths that I want to convert into data frames.我有一个要转换为数据框的文件路径列表。
Here is what the files look like这是文件的样子
To better help organize it I would like to have a dict where the key is the dates, and the values are a dict where their keys are the names and they have a dict where keys are results, sales, team, and the values are a dataframe of the file.为了更好地帮助组织它,我想要一个字典,其中键是日期,值是一个字典,其中键是名称,他们有一个字典,其中键是结果、销售、团队,值是文件的 dataframe。
I hope I explained it well.我希望我解释得很好。
2022-03-23_John_result_data.csv
2022-03-23_John_sales_data.csv
2022-03-23_John_team_data.csv
2022-03-23_Lisa_result_data.csv
2022-03-23_Lisa_sales_data.csv
2022-03-23_Lisa_team_data.csv
2022-03-23_Troy_result_data.csv
2022-03-23_Troy_sales_data.csv
2022-03-23_Troy_team_data.csv
2022-03-25_Bart_result_data.csv
2022-03-25_Bart_sales_data.csv
2022-03-25_Bart_team_data.csv
EDIT编辑
Sorry for the edit but assume the file name could be '2022-03-23_John love [23]_result_data.csv'] forgot to add this case where they could have a space between the names.抱歉进行了编辑,但假设文件名可能是“2022-03-23_John love [23]_result_data.csv”] 忘记添加这种情况,名称之间可能有空格。
You could probably iterate over the file names and do multiple dict.setdefault
s (or use a defaultdict), eg:您可能会遍历文件名并执行多个dict.setdefault
s(或使用 defaultdict),例如:
filenames = ['2022-03-23_John_result_data.csv']
dfs = {}
for filename in filenames:
date, name, category, _ = filename.split('_', 3)
dfs.setdefault(date, {}).setdefault(name, {})[category] = pd.read_csv(filename)
Or using a defaultdict
...或者使用defaultdict
...
from collections import defaultdict从 collections 导入 defaultdict
dfs = defaultdict(dict)
Then your dfs.setdefault(...)
line becomes:然后你的dfs.setdefault(...)
行变成:
dfs[date][name][category] = pd.read_csv(filename)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.