【问题标题】:Loop to match dictionary keys from list append dictionary with associated data in other columns循环以匹配列表中的字典键附加字典与其他列中的关联数据
【发布时间】:2021-12-30 04:02:26
【问题描述】:

我想遍历我的数据并使用“事件”值及其对应的“xCordAdjusted”和“yCordAdjusted”填充我的字典

数据框:

season  period  teamCode    event   goal    xCord   xCordAdjusted   yCord   yCordAdjusted   shotType    playerPositionThatDidEvent  playerNumThatDidEvent   shooterPlayerId shooterName shooterLeftRight    
2014    1            MTL    MISS    0           61             61     29              29  WRIST                             C                     51    8471976.0   David Desharnais    L  
2014    1            TOR    SHOT    0          -54             54     29             -29  BACK                              C                     42    8475098.0   Tyler Bozak         R
2014    1            TOR    SHOT    0          -40             40     32             -32  WRIST                             D                     46    8471392.0   Roman Polak         R

我的作品:

league_data = {};
league_data['SHOT'] = {};
league_data['SHOT']['x'] = [];
league_data['SHOT']['y'] = [];
league_data['GOAL'] = {};
league_data['GOAL']['x'] = [];
league_data['GOAL']['y'] = [];
league_data['MISS'] = {};
league_data['MISS']['x'] = [];
league_data['MISS']['y'] = [];
event_types = ['SHOT','GOAL','MISS']

for data in season_df:
    for event in event_types:
        if data in event_types:
             if 'x' in range(0,100):
                league_data[event]['x'].append(['xCordAdjusted'])
                league_data[event]['y'].append(['yCordAdjusted'])
league_data

输出:

{'SHOT': {'x': [], 'y': []},
 'GOAL': {'x': [], 'y': []},
 'MISS': {'x': [], 'y': []}}

【问题讨论】:

    标签: python python-3.x list dictionary for-loop


    【解决方案1】:

    您可以直接从 DataFrame 中以矢量化方式提取所需信息,而不是重复循环:

    league_data = {
        'SHOT': {},
        'GOAL': {},
        'MISS': {},
    }
    
    for event in event_types:
        mask = (season_df['event'] == event) & season_df['xCord'].between(0, 100)
        x_adjusted = season_df.loc[mask, 'xCordAdjusted'].tolist()
        y_adjusted = season_df.loc[mask, 'yCordAdjusted'].tolist()
        league_data[event]['x'] = x_adjusted
        league_data[event]['y'] = y_adjusted
    

    给予

    {'GOAL': {'x': [], 'y': []},
     'MISS': {'x': [61], 'y': [-29]},
     'SHOT': {'x': [], 'y': []}
    }
    

    请注意,我调整了范围条件,因为您的原始代码 if 'x' in range(0,100) 没有达到您的预期,因为它根本没有引用您的 DataFrame。

    【讨论】:

      【解决方案2】:

      for data in season_df: 迭代列,而不是行。
      相反,请使用for index, row in season_df.iterrows()

      但是,对行的迭代非常慢,所以如果您的数据非常大,您可以利用矢量化。

      此外,您的代码看起来不像您预期​​的那样工作......就像if 'x' in range(0, 100)。我根据我的假设重新编码,试试这个。

      for event in event_types:
          matched_df = season_df[season_df['event'] == event]
          x_matched_list = matched_df[(0 <= matched_df['xCordAdjusted']) & (matched_df['xCordAdjusted'] <= 100)]['xCordAdjusted'].tolist()
          league_data[event]['x'] = x_matched_list # or extend
          
          y_matched_list = matched_df[(0 <= matched_df['yCordAdjusted']) & (matched_df['yCordAdjusted'] <= 100)]['yCordAdjusted'].tolist()
          league_data[event]['y'] = y_matched_list # or extend
      

      但请注意长度“xCordAdjusted”与“yCordAdjusted”不匹配的可能性

      【讨论】:

      • “ValueError:长度必须匹配才能比较”我该如何解决?
      • 你能告诉我更详细的错误信息吗?它对我有用。
      猜你喜欢
      • 1970-01-01
      • 2019-10-26
      • 2018-04-29
      • 2021-12-03
      • 2014-07-06
      • 2017-05-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多