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
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)
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)