0

我有一个看起来像这样的熊猫数据框。

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 

只返回一组值。我很确定缩进是正确的。

为什么要这样做,我怎样才能让它返回字典?

4

1 回答 1

4

最好使用 pandas groupby:

# Limit the week
limited = frame[frame['week'] <= n]
# Calculate the home and away points
home_points= limited.groupby('home').sum()['homePTS']
away_points = limited.groupby('away').sum()['awayPTS']
# Sum them
home_points.add(away_points, fill_value=0)

这给了我(对于 n = 3):

ATL     88
CHI    265
DAL    110
DET    185
HOU    179
MIA     95
SEA     95
STL    185
TOR     99

要修复您的解决方案,您可以将每个迭代附加到列表中:

uniqueTeams = set(frame['home']) | set(frame['away'])

def _earlyGamesPTS(n):
    all_teams = []
    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
        all_teams.append([team, totalPTS])
    return all_teams

其中(对于相同的 n = 3)给出:

[['TOR', 99],
 ['DAL', 110],
 ['STL', 185],
 ['CHI', 265],
 ['ATL', 88],
 ['DET', 185],
 ['MIA', 95],
 ['HOU', 179],
 ['SEA', 95]]
于 2013-09-18T13:13:29.363 回答