提问人:WillyTheWalrus_123 提问时间:7/10/2023 最后编辑:WillyTheWalrus_123 更新时间:7/10/2023 访问量:34
无法从嵌套列表中提取值并转换为数据帧
Having trouble extracting values from a nested list and converting to dataframe
问:
我正在尝试创建一个包含“团队”、“比赛”、“胜利”、“失败”和“平局”的数据帧。
下面是数据片段:
[{'away_games': {'games': 4, 'losses': 2, 'ties': 0, 'wins': 2},
'conference': 'Mountain West',
'conference_games': {'games': 8, 'losses': 3, 'ties': 0, 'wins': 5},
'division': 'Mountain',
'expected_wins': 9.9,
'home_games': {'games': 7, 'losses': 1, 'ties': 0, 'wins': 6},
'team': 'Air Force',
'total': {'games': 13, 'losses': 3, 'ties': 0, 'wins': 10},
'year': 2022},
{'away_games': {'games': 8, 'losses': 6, 'ties': 0, 'wins': 1},
'conference': 'Mid-American',
'conference_games': {'games': 9, 'losses': 7, 'ties': 0, 'wins': 1},
'division': 'East',
'expected_wins': 1.5,
'home_games': {'games': 5, 'losses': 4, 'ties': 0, 'wins': 1},
'team': 'Akron',
'total': {'games': 13, 'losses': 10, 'ties': 0, 'wins': 2},
'year': 2022},
这是我尝试的代码:
# Create an empty DataFrame
df = pd.DataFrame(columns=['team', 'games', 'wins', 'losses', 'ties'])
# Loop through each record in the data
for record in data:
try:
# Extract the desired values
team = record['team']
games = record['total'].get['games']
wins = record['total'].get['wins']
losses = record['total'].get['losses']
ties = record['total'].get['ties']
# Create a new row with the extracted values
new_row = {'team': team, 'games': games, 'wins': wins, 'losses': losses, 'ties': ties}
# Append the new row to the DataFrame
df = df.append(new_row, ignore_index=True)
except KeyError as e:
print(f"Skipping record due to missing key: {e}")
# Print the resulting DataFrame
print(df)
我收到一个错误,指出“TeamRecord”对象不可下标。
我相信有更好/更容易的方法可以做到这一点。任何建议将不胜感激。
答:
0赞
Bracula
7/10/2023
#1
这就是它应该的样子:
import pandas as pd
data=[{'away_games': {'games': 4, 'losses': 2, 'ties': 0, 'wins': 2},
'conference': 'Mountain West',
'conference_games': {'games': 8, 'losses': 3, 'ties': 0, 'wins': 5},
'division': 'Mountain',
'expected_wins': 9.9,
'home_games': {'games': 7, 'losses': 1, 'ties': 0, 'wins': 6},
'team': 'Air Force',
'total': {'games': 13, 'losses': 3, 'ties': 0, 'wins': 10},
'year': 2022},
{'away_games': {'games': 8, 'losses': 6, 'ties': 0, 'wins': 1},
'conference': 'Mid-American',
'conference_games': {'games': 9, 'losses': 7, 'ties': 0, 'wins': 1},
'division': 'East',
'expected_wins': 1.5,
'home_games': {'games': 5, 'losses': 4, 'ties': 0, 'wins': 1},
'team': 'Akron',
'total': {'games': 13, 'losses': 10, 'ties': 0, 'wins': 2},
'year': 2022}]
rows = []
# Loop through each record in the data
for record in data:
try:
# Extract the desired values
team = record['team']
games = record['total']['games']
wins = record['total']['wins']
losses = record['total']['losses']
ties = record['total']['ties']
# Create a new row with the extracted values
new_row = {'team': team, 'games': games, 'wins': wins, 'losses': losses, 'ties': ties}
rows.append(new_row)
except KeyError as e:
print(f"Skipping record due to missing key: {e}")
# Print the resulting DataFrame
df = pd.DataFrame(rows, columns=['team', 'games', 'wins', 'losses', 'ties'])
print(df)
team games wins losses ties
0 Air Force 13 10 3 0
1 Akron 13 2 10 0
看起来您的数据也很无聊,因为赢、输和平局的总和必须导致所玩的游戏总数。但事实并非如此。Akron
您不使用 ,另请参阅通过一次追加一行来创建 Pandas 数据帧,该行已在 中弃用并删除。在大多数情况下,在循环中追加是一种不好的做法。get
append
Pandas>=2.0.0
评论
0赞
WillyTheWalrus_123
7/10/2023
感谢您的输入!数据与来源匹配,所以我会让维护者知道它 - collegefootballdata.com/exporter/......重新运行后,我仍然收到相同的 TypeError:“TeamRecord”对象不可下标
0赞
Bracula
7/10/2023
@WillyTheWalrus_123 它现在能解决你的问题吗?呃,我没有看到你的编辑。
0赞
Bracula
7/10/2023
@WillyTheWalrus_123我已经编辑了答案,您能否尝试在清晰的 Python 实例中运行代码?
0赞
WillyTheWalrus_123
7/10/2023
如上所述,当“数据”被显式传递时,您的代码可以完美运行。谢谢你。我更新了我的完整列表以像这样引用,现在它适用于整个事情,并且我避免了 typeerror 消息。
评论
TeamRecord
既不在示例数据中,也不在提供的代码中。在抛出错误的行上,代码是什么?