返回空数据框

时间:2019-07-06 09:07:22

标签: python pandas python-2.7 dataframe

我正在尝试创建一个带有以下列的数据框:Home给予主队,Away给予客队,hPassYds给予主场传球码,aPassYds给予客队的传球码

我创建了一个空的数据框,并尝试通过teamStats函数将其追加到数据框中,但是我得到了一个空的数据框,但未添加任何值。帮助。

代码:

teams= ['KC','NYJ','ATL','BAL','PIT','ARI','JAX','OAK','PHI','IND','SEA','CAR','NYG','NO','LAC']
df= pd.DataFrame(columns =["Home","Away","hPassYds","aPassYds"])

def teamStats(h,df):
    game = nflgame.one(2017,1,h,h)
    hteam = game.home
    ateam = game.away
    hpass_yds = game.stats_home.passing_yds
    apass_yds = game.stats_away.passing_yds
    df = df.append({'Home':hteam,'Away':ateam,'hPassYds':hpass_yds,'aPassYds':apass_yds}, ignore_index=True)

for h in teams:
    teamStats(h,df)
print df

Empty DataFrame
Columns: [Home, Away, hPassYds, aPassYds]
Index: []

1 个答案:

答案 0 :(得分:0)

我已重写您的代码以获取结果:-

teams= [['KC','NYJ','ATL','BAL'],['PIT','ARI','JAX','OAK'],['PHI','IND','SEA','CAR'],['NYG','NO','LAC',] ]

df= pd.DataFrame(data=teams, columns =["Home","Away","hPassYds","aPassYds"])   
# Here you are making mistake, you have to pass `teams` list
print(df)

def teamStats(h,df):
    game = nflgame.one(2017,1,h,h)
    hteam = game.home
    ateam = game.away
    hpass_yds = game.stats_home.passing_yds
    apass_yds = game.stats_away.passing_yds
    df = df.append({'Home':hteam,'Away':ateam,'hPassYds':hpass_yds,'aPassYds':apass_yds}, ignore_index=True)

for h in teams:
    teamStats(h,df)
print (df)

我希望它可以解决您的问题。