我有一个看起来像这样的熊猫数据框。
frame = pd.DataFrame({'home' : ['CHI', 'ATL', 'SEA', 'DET', 'STL','HOU' ,'CHI','CHI'],
'away' : ['DET', 'CHI', 'HOU', 'TOR', 'DAL', 'STL', 'MIA', 'SEA'],
'awayPTS' : [88, 75, 105, 99, 110, 85, 95, 100],
'homePTS' : [92, 88, 95, 97, 100, 74, 98, 110],
'week' : [1, 1, 1, 2, 2,3, 3, 4]})
我可以通过这样做得到我想要的输出
uniqueTeams = frame['home'].unique()
def _earlyGamesPTS(n):
for team in uniqueTeams:
homePTS = frame['homePTS'][(frame.week <= n) & (frame.home == team)].sum()
awayPTS = frame['awayPTS'][(frame.week <= n) & (frame.away == team)].sum()
totalPTS = homePTS+ awayPTS
print team, totalPTS
运行该函数会打印所需的信息。
我无法让函数返回我想要的值。
def _earlyGamesPTS(n):
for team in uniqueTeams:
homePTS = frame['homePTS'][(frame.week <= n) & (frame.home == team)].sum()
awayPTS = frame['awayPTS'][(frame.week <= n) & (frame.away == team)].sum()
totalPTS = homePTS+ awayPTS
return team, totalPTS
只返回一组值。我很确定缩进是正确的。
为什么要这样做,我怎样才能让它返回字典?