I have a lot of dataframes that I would like to group into dictionaries based on prefixes and suffixes:
prefixes = ['one_season_bucket',
'two_season_bucket',
'three_season_bucket',
'four_season_bucket']
suffixes:
suffixes = ['racer_bio',
'spring_rate',
'neaps_rate',
'spring_raw',
'neap_raw',
'opposing_team',
'opposing_team_distribution',
'stern_score',
'bow_score',
'team_score']
example of some dataframe names:
...
two_season_bucket_year1_racer_bio
two_season_bucket_year1_spring_rate
two_season_bucket_year1_neaps_rate
two_season_bucket_year1_spring_raw
two_season_bucket_year1_neap_raw
two_season_bucket_year1_opposing_team
two_season_bucket_year1_opposing_team_distribution
two_season_bucket_year1_stern_score
two_season_bucket_year1_bow_score
two_season_bucket_year1_team_score
four_season_bucket_year4_racer_bio
four_season_bucket_year4_spring_rate
...
four_season_bucket_year4_neaps_rate
four_season_bucket_year4_spring_raw
four_season_bucket_year4_neap_raw
four_season_bucket_year4_opposing_team
four_season_bucket_year4_opposing_team_distribution
four_season_bucket_year4_stern_score
four_season_bucket_year4_bow_score
four_season_bucket_year4_team_score
basically I want to use these lists to make dictionaries with the dataframe name as the key and the data frame as the value, broken out by prefixes, inclduing all the suffixes, such as:
two_season_bucket_suffixes = {'two_season_bucket_year1_racer_bio':two_season_bucket_year1_racer_bio,
'two_season_bucket_year1_spring_rate':two_season_bucket_year1_spring_rate,
'two_season_bucket_year1_neaps_rate':two_season_bucket_year1_neaps_rate,
'two_season_bucket_year1_spring_raw':two_season_bucket_year1_spring_raw,
'two_season_bucket_year1_neap_raw':two_season_bucket_year1_neap_raw,
'two_season_bucket_year1_opposing_team':two_season_bucket_year1_opposing_team,
'two_season_bucket_year1_opposing_team_distribution':two_season_bucket_year1_opposing_team_distribution,
'two_season_bucket_year1_stern_score':two_season_bucket_year1_stern_score,
'two_season_bucket_year1_bow_score':two_season_bucket_year1_bow_score,
'two_season_bucket_year1_team_score':two_season_bucket_year1_team_score,
'two_season_bucket_year2_racer_bio':two_season_bucket_year2_racer_bio,
'two_season_bucket_year2_spring_rate':two_season_bucket_year2_spring_rate,
'two_season_bucket_year2_neaps_rate':two_season_bucket_year2_neaps_rate,
'two_season_bucket_year2_spring_raw':two_season_bucket_year2_spring_raw,
'two_season_bucket_year2_neap_raw':two_season_bucket_year2_neap_raw,
'two_season_bucket_year2_opposing_team':two_season_bucket_year2_opposing_team,
'two_season_bucket_year2_opposing_team_distribution':two_season_bucket_year2_opposing_team_distribution,
'two_season_bucket_year2_stern_score':two_season_bucket_year2_stern_score,
'two_season_bucket_year2_bow_score':two_season_bucket_year2_bow_score,
'two_season_bucket_year2_team_score':two_season_bucket_year2_team_score}
so I do this with this code:
from itertools import product
years = ['year1', 'year2']
prefix = 'two_season_bucket'
two_season_bucket_suffixes = {'%s_%s_%s' %(pre, y, suff): eval('%s_%s_%s' %(pre, y, suff)) for y, suff in product(years, suffixes)}
however some of the buckets do not exist, for example 'two_season_bucket_year2_opposing_team' might not exist. Everytime I run the code certain ones may not exist. So I want to put in something to ignore any dataframes that might not exist and continue making the dictionary:
two_season_bucket_suffixes = try:
{'%s_%s_%s' %(pre, y, suff): eval('%s_%s_%s' %(pre, y, suff)) for y, suff in product(years, suffixes)}
except:
pass
but I don't think I am doing it correctly.