1

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.

elcunyado
  • 351
  • 1
  • 2
  • 11
  • yeah you guessed right, try is a statement that should wrap code in a block its not a function that returns a value, you can't assign a variable to your try statement. Define an empty dictionnary, write a simple loop (no comprehension), and at each step of the loop assign the value you want to your dictionnary. To wrap it in a try except statement, simply wrap your assignment to the dictionnary with try except statement. Edit: this is the same logic behind @piyush's answer – Youyoun Oct 14 '20 at 09:07
  • Avoid using general except statements like this. If another error occurs you wont be able to see it, so use `except YourException:`. You wont be able to interrupt your code if the loop is taking too much time, unless you kill the whole process (simply because KeyboardInterrupt is an exception, and you asked your program to pass when it receives any kind of exception). – Youyoun Oct 14 '20 at 09:09
  • "however some of the buckets do not exist, for example 'two_season_bucket_year2_opposing_team' might not exist" Instead of trying to do this, you should fix the code that assigns to those variables so that it inserts directly into the dictionary instead. Consider for example the advice in https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables, and https://www.reddit.com/r/learnpython/wiki/faq#wiki_how_do_i_make_variable_variables.3F – Karl Knechtel Oct 14 '20 at 09:41

3 Answers3

1

Solution:
This code snippet might solve your issue:

def return_keyval_pair(years, suffixes):
    final_dict = {}
    for y, suff in product(years, suffixes):
        try:
            final_dict['%s_%s_%s' %(pre, y, suff)] = eval('%s_%s_%s' %(pre, y, suff))
        except:
            pass
    return final_dict

two_season_bucket_suffixes = return_keyval_pair(years, suffixes)
Piyush Sambhi
  • 843
  • 4
  • 13
0

The code for the try ... except construction works like this:

try:
   ...
except:
   ...

So your code would become:

try:
   two_season_bucket_suffixes = {'%s_%s_%s' %(pre, y, suff): eval('%s_%s_%s' %(pre, y, suff)) for y, suff in product(years, suffixes)}
except:
   pass

This will however fali to catch any excpetion happing in the code. Ideally you would want to skip errors related to 'two_season_bucket_year2_bow_score' not existing. You can find which excpetion is realted to your problem by looking into the error feed of your python console. However i image it will be a KeyError in which case your code will beocme:

try:
   two_season_bucket_suffixes = {'%s_%s_%s' %(pre, y, suff): eval('%s_%s_%s' %(pre, y, suff)) for y, suff in product(years, suffixes)}
except KeyError:
   pass
Gozy4
  • 444
  • 6
  • 11
0
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) if '%s_%s_%s' %(pre, y, suff) in globals()}
辜乘风
  • 152
  • 5