第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

在循環(huán)結(jié)束時(shí)將行添加到 pandas 數(shù)據(jù)框

在循環(huán)結(jié)束時(shí)將行添加到 pandas 數(shù)據(jù)框

qq_遁去的一_1 2023-06-20 13:29:36
我正在嘗試在數(shù)據(jù)框中添加行作為循環(huán)的一部分。該程序循環(huán)訪問(wèn) URL 并以數(shù)據(jù)幀格式提取數(shù)據(jù)for id in game_ids:    df_team_final = []    df_player_final = []    url = 'https://www.fibalivestats.com/data/' + id + '/data.json'    content = requests.get(url)    data = json.loads(content.content)在循環(huán)結(jié)束時(shí),我使用 concat 合并客隊(duì)/主隊(duì)(和球員)的兩個(gè) df    team_full = pd.concat([df_home_team, df_away_team])    player_full = pd.concat([df_home_player_merge, df_away_player_merge])在循環(huán)外我已經(jīng)編程保存為 Excel# #if cant find it, create new spread sheetwriter = 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()當(dāng)我循環(huán)瀏覽多個(gè)網(wǎng)頁(yè)時(shí),我需要隨時(shí)更新 df,顯然在當(dāng)前格式中,我只是用第二個(gè)循環(huán)覆蓋第一個(gè) url在循環(huán)結(jié)束時(shí)附加或添加到數(shù)據(jù)幀的最佳方法是什么?
查看完整描述

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)中添加了容器:

http://img1.sycdn.imooc.com//649139600001069504090178.jpg

但是你應(yīng)該把它們放在循環(huán)開(kāi)始之前:


# initialize them here

df_team_final = []

df_player_final = []

for id in game_ids:


查看完整回答
反對(duì) 回復(fù) 2023-06-20
  • 1 回答
  • 0 關(guān)注
  • 159 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)