获取数据帧列表并按变量分组,然后将该变量用作字典的键 [英] Taking a list of data frames and grouping by a variable and using that variable as the key to a dictionary
问题描述
我是python编程的新手.我有一个熊猫数据框的列表,所有的数据框都具有年份"列.我正在尝试按该列分组并转换为字典,其中字典键是变量"Year",而值是该年的数据帧列表.在python中有可能吗?
I am relatively new to python programming. I have a list of pandas dataframes that all have the column 'Year'. I am trying to group by that column and convert to a dictionary where the dictionary key is the variable 'Year' and values is a list of dataframes of that year. Is this possible in python?
我尝试过:
grouped_dict = list_of_csv_files.groupby(by = 'Year').to_dict()
我相信我将不得不遍历每个数据帧吗?我没有提供任何数据,因为我希望它是一个简单的解决方案.
I believe I will have to loop through each dataframe? I did not provide any data because I am hoping it is a somewhat simple solution.
我也尝试过:
grouped_dict = list_of_csv_files.groupby(by = 'Year').apply(lambda dfg: dfg.to_dict(orient='list')).to_dict()
任何指导将不胜感激!
推荐答案
到目前为止,其他答案都未达到要求,所以我给您一个替代方法.假设您有CSV文件(因为您的变量是以这种方式命名的):
Other answers have missed the mark so far, so I'll give you an alternative. Assuming you have CSV files (since your variable is named that way):
from collections import defaultdict
yearly_dfs = defaultdict(list)
for csv in list_of_csv_files:
df = pd.read_csv(csv)
for yr, yr_df in df.groupby("Year"):
yearly_dfs[yr].append(yr_df)
假设您已经有DataFrames:
Assuming you have DataFrames already:
from collections import defaultdict
yearly_dfs = defaultdict(list)
for df in list_of_csv_files:
for yr, yr_df in df.groupby("Year"):
yearly_dfs[yr].append(yr_df)
这篇关于获取数据帧列表并按变量分组,然后将该变量用作字典的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!