1 回答
TA贡献1735条经验 获得超5个赞
由于我们看不到完整的代码,因此我只能在这里给出一个粗略的轮廓。
我假设您没有将抓取的数据附加到某种容器,因此它会在下一次迭代后丢失。
# empty lists outside of loop to store data
df_team_final = []
df_player_final = []
for id in game_ids:
url = 'https://www.fibalivestats.com/data/' + id + '/data.json'
content = requests.get(url)
data = json.loads(content.content)
# create dataframes that you need
# df_home_team, df_away_team etc
# and append data to containers
team_full = pd.concat([df_home_team, df_away_team])
player_full = pd.concat([df_home_player_merge, df_away_player_merge])
df_team_final.append(team_full)
df_player_final.append(player_full )
现在您将数据框存储为列表,您可以将它们与pandas.concat
# outside of the loop
team_full = pd.concat(df_team_final)
player_full = pd.concat(df_player_final)
并立即保存:
writer = pd.ExcelWriter('Box Data.xlsx', engine='openpyxl')
team_full.to_excel(writer, sheet_name='Team Stats', index=False)
player_full.to_excel(writer, sheet_name='Player Stats', index=False)
writer.save()
writer.close()
编辑
从您共享的文件中,我看到您在循环中添加了容器:
但是你应该把它们放在循环开始之前:
# initialize them here
df_team_final = []
df_player_final = []
for id in game_ids:
添加回答
举报