作成しようとしているマスターデータフレームループのコードです。ループとデータフレームをマージする
import requests
import pandas as pd
"""
from: http://stats.nba.com/league/player/#!/advanced/
"""
u_a = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.82 Safari/537.36"
advanced = "http://stats.nba.com/stats/leaguedashplayerstats?College=&Conference=&Country=&DateFrom=&DateTo=&Division=&DraftPick=&DraftYear=&GameScope=&GameSegment=&Height=&LastNGames=0&LeagueID=00&Location=&MeasureType=Advanced&Month=0&OpponentTeamID=0&Outcome=&PORound=0&PaceAdjust=N&PerMode=Totals&Period=0&PlayerExperience=&PlayerPosition=&PlusMinus=N&Rank=N&Season=2016-17&SeasonSegment=&SeasonType=Regular+Season&ShotClockRange=&StarterBench=&TeamID=0&VsConference=&VsDivision=&Weight="
passing = "http://stats.nba.com/stats/leaguedashptstats?College=&Conference=&Country=&DateFrom=&DateTo=&Division=&DraftPick=&DraftYear=&GameScope=&Height=&LastNGames=0&LeagueID=00&Location=&Month=0&OpponentTeamID=0&Outcome=&PORound=0&PerMode=PerGame&PlayerExperience=&PlayerOrTeam=Player&PlayerPosition=&PtMeasureType=Possessions&Season=2016-17&SeasonSegment=&SeasonType=Regular+Season&StarterBench=&TeamID=0&VsConference=&VsDivision=&Weight="
scoring = "http://stats.nba.com/stats/leaguedashplayerstats?College=&Conference=&Country=&DateFrom=&DateTo=&Division=&DraftPick=&DraftYear=&GameScope=&GameSegment=&Height=&LastNGames=0&LeagueID=00&Location=&MeasureType=Scoring&Month=0&OpponentTeamID=0&Outcome=&PORound=0&PaceAdjust=N&PerMode=PerGame&Period=0&PlayerExperience=&PlayerPosition=&PlusMinus=N&Rank=N&Season=2016-17&SeasonSegment=&SeasonType=Regular+Season&ShotClockRange=&StarterBench=&TeamID=0&VsConference=&VsDivision=&Weight="
url_list = [advanced,passing,scoring]
master_df = []
for i in url_list:
r = requests.get(i, headers={"USER-AGENT":u_a})
r.raise_for_status()
headers = []
for item in r.json()['resultSets']:
for val in item['headers']:
headers.append(val)
df = []
for item in r.json()['resultSets']:
for row in item['rowSet']:
row_df = []
for val in row:
row_df.append(val)
df.append(row_df)
master_df.append(df)
ループは機能しますが、ループは動作しますが、各データセットは別のセットの上にスタックします。データがマージされ、同一の列がコピーされないようにしたい場合は、各JSONファイルの新しいデータが追加の列に追加されます。私はまた、ヘッダーが新しい場合は列名を追加するだけです。