遍历列表中的字典并将行添加到DataFrame

时间:2020-05-10 13:32:51

标签: python loops dictionary

我有一个清单,试图从嵌套在另一个清单中的字典中解包。

然后我想将该行添加到DataFrame中。

headers = ['minutesPlayed', 'goals', 'goalsAssist', 'shotsOnTarget',
           'shotsOffTarget', 'shotsBlocked', 'hitWoodwork', 'totalContest', 
           'penaltyMiss', 'penaltyWon', 'bigChanceMissed'] 

Python Variables

我尝试过的代码是:

rows = []
for groups in data['groups']:
    row = []

    #Summary
    row.append(groups['minutesPlayed'])
    row.append(groups['goals'])
    row.append(groups['goalAssist'])

    #Attack
    row.append(groups['shotsOnTarget'])
    row.append(groups['shotsOffTarget'])
    row.append(groups['shotsBlocked'])
    row.append(groups['hitWoodwork'])
    row.append(groups['totalContest'])
    row.append(groups['penaltyMiss'])
    row.append(groups['penaltyWon'])
    row.append(groups['bigChanceMissed'])
    rows.append(row)

df = pd.DataFrame(rows, columns=headers)

但是我收到错误:

KeyError: 'shotsOnTarget' 

不允许我迭代组列表中的第二个元素。

有什么提示吗?

EDIT添加了数据打印组[group]:

print(data['groups'])
[{'minutesPlayed': "89'", 'goals': '0', 'goalAssist': '1', 'statisticsItems': [{'minutesPlayed': 'Minutes played'}, {'goals': 'Goals'}, {'goalAssist': 'Assists'}], 'groupName': 'Summary'}, {'shotsOnTarget': '0', 'shotsOffTarget': '0', 'shotsBlocked': '1', 'hitWoodwork': '0', 'totalContest': '1 (0)', 'goals': '0', 'goalAssist': '1', 'penaltyMiss': '0', 'penaltyWon': '0', 'bigChanceMissed': '0', 'statisticsItems': [{'shotsOnTarget': 'Shots on target'}, {'shotsOffTarget': 'Shots off target'}, {'shotsBlocked': 'Shots blocked'}, {'totalContest': 'Dribble attempts (succ.)'}], 'groupName': 'Attack'}, {'touches': 55, 'accuratePass': '26 (70.3%)', 'keyPass': '1', 'totalCross': '0 (0)', 'totalLongBalls': '2 (0)', 'bigChanceCreated': '0', 'statisticsItems': [{'touches': 'Touches'}, {'accuratePass': 'Passes (acc.)'}, {'keyPass': 'Key passes'}, {'totalCross': 'Crosses (acc.)'}, {'totalLongBalls': 'Long balls (acc.)'}], 'groupName': 'Passing'}, {'possessionLost': '26', 'groundDuels': '9 (0)', 'aerialDuels': '3 (1)', 'wasFouled': '0', 'fouls': '2', 'offsides': '0', 'statisticsItems': [{'groundDuels': 'Ground duels (won)'}, {'aerialDuels': 'Aerial duels (won)'}, {'possessionLost': 'Possession lost'}, {'fouls': 'Fouls'}, {'wasFouled': 'Was fouled'}], 'groupName': 'Other'}, {'totalClearance': '0', 'clearanceOffLine': '0', 'blockedScoringAttempt': '0', 'interceptionWon': '0', 'totalTackle': '0', 'challengeLost': '1', 'lastManTackle': '0', 'errorLeadToShot': '0', 'errorLeadToGoal': '0', 'ownGoals': '0', 'penaltyConceded': '0', 'statisticsItems': [{'totalClearance': 'Clearances'}, {'blockedScoringAttempt': 'Blocked shots'}, {'interceptionWon': 'Interceptions'}, {'totalTackle': 'Tackles'}, {'challengeLost': 'Dribbled past'}], 'groupName': 'Defence'}]

1 个答案:

答案 0 :(得分:0)

您的gl_Position = vec4(aPos, 0, 1.0); 是一个列表。现在它们的元素是字典,您的data['groups']minutesPlayedgoals在列表的第一个元素中,因此当您运行goalAssist from for变量时,它们会被调用,但是您的groups和其他人位于第二个元素中。一个更好的方法可以遵循。您不需要for循环。

shotsOnTarget