【问题标题】:Make a dictionary out of all dataframes with certain suffixes and prefixes使用特定后缀和前缀的所有数据帧制作字典
【发布时间】:2021-01-18 21:52:15
【问题描述】:

我有很多数据框,我想根据前缀和后缀将它们分组到字典中:

prefixes = ['one_season_bucket',
'two_season_bucket',
'three_season_bucket',
'four_season_bucket']

后缀:

suffixes = ['racer_bio',
'spring_rate',
'neaps_rate',
'spring_raw',
'neap_raw',
'opposing_team',
'opposing_team_distribution',
'stern_score',
'bow_score',
'team_score']

一些数据框名称的示例:

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

基本上我想使用这些列表来制作字典,以数据框名称为键,数据框为值,由前缀分解,包括所有后缀,例如:

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}

可以这样做吗?使用两个列表创建字典?

【问题讨论】:

  • 您的数据框及其名称当前保存在什么结构中?

标签: python pandas


【解决方案1】:

通过将每个前缀和后缀与itertools.product() 组合来列出前缀和后缀的总组合。将列表分配为字典格式。这符合你的问题吗?

import itertools

dicts = {}
for k in dict_lst:
    dicts[v1+'_'+v2] = v1+'_'+v2
dicts
{'one_season_bucketracer_bio': 'one_season_bucketracer_bio',
 'one_season_bucketspring_rate': 'one_season_bucketspring_rate',
 'one_season_bucketneaps_rate': 'one_season_bucketneaps_rate',
 'one_season_bucketspring_raw': 'one_season_bucketspring_raw',
 'one_season_bucketneap_raw': 'one_season_bucketneap_raw',
 'one_season_bucketopposing_team': 'one_season_bucketopposing_team',
 'one_season_bucketopposing_team_distribution': 'one_season_bucketopposing_team_distribution',
 'one_season_bucketstern_score': 'one_season_bucketstern_score',
 'one_season_bucketbow_score': 'one_season_bucketbow_score',
 'one_season_bucketteam_score': 'one_season_bucketteam_score',
 'two_season_bucketracer_bio': 'two_season_bucketracer_bio',
 'two_season_bucketspring_rate': 'two_season_bucketspring_rate',
 'two_season_bucketneaps_rate': 'two_season_bucketneaps_rate',
 'two_season_bucketspring_raw': 'two_season_bucketspring_raw',
 'two_season_bucketneap_raw': 'two_season_bucketneap_raw',
 'two_season_bucketopposing_team': 'two_season_bucketopposing_team',
 'two_season_bucketopposing_team_distribution': 'two_season_bucketopposing_team_distribution',
 'two_season_bucketstern_score': 'two_season_bucketstern_score',
 'two_season_bucketbow_score': 'two_season_bucketbow_score',
 'two_season_bucketteam_score': 'two_season_bucketteam_score',
 'three_season_bucketracer_bio': 'three_season_bucketracer_bio',
 'three_season_bucketspring_rate': 'three_season_bucketspring_rate',
 'three_season_bucketneaps_rate': 'three_season_bucketneaps_rate',
 'three_season_bucketspring_raw': 'three_season_bucketspring_raw',
 'three_season_bucketneap_raw': 'three_season_bucketneap_raw',
 'three_season_bucketopposing_team': 'three_season_bucketopposing_team',
 'three_season_bucketopposing_team_distribution': 'three_season_bucketopposing_team_distribution',
 'three_season_bucketstern_score': 'three_season_bucketstern_score',
 'three_season_bucketbow_score': 'three_season_bucketbow_score',
 'three_season_bucketteam_score': 'three_season_bucketteam_score',
 'four_season_bucketracer_bio': 'four_season_bucketracer_bio',
 'four_season_bucketspring_rate': 'four_season_bucketspring_rate',
 'four_season_bucketneaps_rate': 'four_season_bucketneaps_rate',
 'four_season_bucketspring_raw': 'four_season_bucketspring_raw',
 'four_season_bucketneap_raw': 'four_season_bucketneap_raw',
 'four_season_bucketopposing_team': 'four_season_bucketopposing_team',
 'four_season_bucketopposing_team_distribution': 'four_season_bucketopposing_team_distribution',
 'four_season_bucketstern_score': 'four_season_bucketstern_score',
 'four_season_bucketbow_score': 'four_season_bucketbow_score',
 'four_season_bucketteam_score': 'four_season_bucketteam_score'}

【讨论】:

    【解决方案2】:

    鉴于您有前缀、后缀和年份,您可以通过 '%s_%s_%s' %(prefix, year, suffix) 为您的字典构建密钥 由于您的 DataFrame 与您构造的键具有相同的名称,因此您可以在同一字符串上使用 eval 来获取它们的值并将它们分配给您的键。 请参阅下面的示例。

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

    【讨论】:

    • 谢谢,这行得通。偶尔其中一种组合不起作用(它不会退出),我怎么能写它来跳过那些不起作用的组合并继续制作字典?
    【解决方案3】:

    第一部分很简单。使用列表推导式,例如

    lst = [ pre + '_' + post for pre in prefixes for post in suffixes]
    dct = dict(zip(lst, lst))
    

    如果你需要创建你可以做的变量(例如)

    for i,k in enumerate(lst):
        vars()[k] = i       # creates the vars and assigns the index as value
    

    我不确定你是否真的想要那个。

    【讨论】:

      猜你喜欢
      • 2016-06-17
      • 1970-01-01
      • 2013-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-04
      相关资源
      最近更新 更多