我一直试图弄清楚我如何使用比赛日期来查看客队和主队比赛了多少天。通过为每个列创建一个Rest列,我试图确定最后一次出现之间的播放时间。我假设必须创建一个列表,但是我不太了解它是如何工作的。
脚本:
import csv
import requests
import datetime
from pprint import pprint
for i in range(20001,21271):
req = requests.get('https://statsapi.web.nhl.com/api/v1/schedule?site=en_nhl&gamePk=20180' + str(i) + '&leaderGameTypes=R&expand=schedule.broadcasts.all,schedule.radioBroadcasts,schedule.teams,schedule.ticket,schedule.game.content.media.epg')
data = req.json()
for item in data['dates']:
date = item['date']
games = item['games']
for game in games:
gamePk = game['gamePk']
season = game['season']
teams = game['teams']
home = teams['home']
home_tm = home['team']['abbreviation']
away = teams['away']
away_tm = away['team']['abbreviation']
print(date, gamePk, away_tm, home_tm)
答案 0 :(得分:1)
首先需要创建数据框:
import pandas as pd
kp = []
for i in range(20001,21271):
req = requests.get('https://statsapi.web.nhl.com/api/v1/schedule?site=en_nhl&gamePk=20180' + str(i) + '&leaderGameTypes=R&expand=schedule.broadcasts.all,schedule.radioBroadcasts,schedule.teams,schedule.ticket,schedule.game.content.media.epg')
data = req.json()
for item in data['dates']:
date = item['date']
games = item['games']
for game in games:
gamePk = game['gamePk']
season = game['season']
teams = game['teams']
home = teams['home']
home_tm = home['team']['abbreviation']
away = teams['away']
away_tm = away['team']['abbreviation']
print date, gamePk, away_tm, home_tm
kp.append([date, gamePk, away_tm, home_tm])
df = pd.DataFrame(kp, columns=['Date','Time','Home', 'Away'])
df.to_csv('matches.csv', sep=' ', header=True, index=False)
当然,您只需要运行一次并将其保存为csc。然后运行脚本...
def find_last(match_date, da, team):
home_play = da[da['Home'] == team].tail(1) #then find last matches played at home, select greatest
away_play = da[da['Away'] == team].tail(1) #" " find last matches played at away, select greatest
#then take the last match played, either home or away, whichever is more recent
if home_play.empty and away_play.empty:
print team, "no_matches before this date"
last_match = 'NA'
elif home_play.empty:
last_match = away_play.Date.item()
elif away_play.empty:
last_match = home_play.Date.item()
else:
last_match = max([home_play.Date.item(), away_play.Date.item()])
if last_match != 'NA':
#And then subtract this from "todays" date (match_date)
duration_since_last = pd.to_datetime(match_date) - pd.to_datetime(last_match)
print "Team:", team
print "Todays game date = ", match_date
print "Last match played = ", last_match
print "Rest Period = ", duration_since_last
print
return duration_since_last
df = pd.read_csv('matches.txt', sep=' ')
for k in df.index:
home_team = df.Home[k]
away_team = df.Away[k]
match_date = df.Date[k]
#we want to find all date values less than todays match date.
da = df[df['Date'] < match_date]
if not da.empty:
for team in [home_team,away_team]:
print "Record", k, home_team, 'vs', away_team
find_last(match_date, da, team)
print '________________________________________'
您应该最终得到剩下的日子:
________________________________________
Record 54 WPG vs NSH
Team: WPG
Todays game date = 2018-10-11
Last match played = 2018-10-09
Rest Period = 2 days 00:00:00
Record 54 WPG vs NSH
Team: NSH
Todays game date = 2018-10-11
Last match played = 2018-10-09
Rest Period = 2 days 00:00:00
________________________________________
Record 55 CHI vs MIN
Team: CHI
Todays game date = 2018-10-11
Last match played = 2018-10-07
Rest Period = 4 days 00:00:00
Record 55 CHI vs MIN
Team: MIN
Todays game date = 2018-10-11
Last match played = 2018-10-06
Rest Period = 5 days 00:00:00
________________________________________