1 回答

TA貢獻(xiàn)1735條經(jīng)驗(yàn) 獲得超5個(gè)贊
由于我們看不到完整的代碼,因此我只能在這里給出一個(gè)粗略的輪廓。
我假設(shè)您沒(méi)有將抓取的數(shù)據(jù)附加到某種容器,因此它會(huì)在下一次迭代后丟失。
# 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 )
現(xiàn)在您將數(shù)據(jù)框存儲(chǔ)為列表,您可以將它們與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()
編輯
從您共享的文件中,我看到您在循環(huán)中添加了容器:
但是你應(yīng)該把它們放在循環(huán)開(kāi)始之前:
# initialize them here
df_team_final = []
df_player_final = []
for id in game_ids:
添加回答
舉報(bào)