我有一个像这样的熊猫数据框。
frame = pd.DataFrame({'home' : ['CHI', 'ATL', 'SEA', 'DET', 'STL','HOU' ,'CHI','CHI'],
'away' : ['DET', 'CHI', 'HOU', 'TOR', 'DAL', 'STL', 'MIA', 'SEA']})
多亏了unutbu,我可以像这样保持每支球队的总比赛总数。
awayGP = collections.Counter()
homeGP = collections.Counter()
def count_games():
for idx, row in frame.iterrows():
homeGP[row['home']] +=1
awayGP[row['away']] +=1
test = homeGP + awayGP
yield awayGP[row['away']], awayGP[row['home']], , homeGP[row['away']], homeGP[row['home']]
frame['awayteamAwayGP'] , frame['hometeamAwayGP'], frame['awayteamHomeGP'], frame['hometeamHomeGP'] = zip(*list(count_games()))
frame['awayteamGames'] = frame['awayteamAwayGP'] + frame['awayteamHomeGP']
frame['hometeamGames'] = frame['hometeamAwayGP'] + frame['hometeamHomeGP']
del frame['awayteamAwayGP'] , frame['hometeamAwayGP'], frame['awayteamHomeGP'], frame['hometeamHomeGP']
我希望能够保持每支球队得分的总和。
frame['awayPTS'] = [88, 75, 105, 99, 110, 85, 95, 100]
frame['homePTS'] = [92, 88, 95, 97, 100, 74, 98, 110]
这是所需的输出。
away home awayteamGP hometeamGP awayPTS homePTS awayteam_totalPTS hometeam_totalPTS
DET CHI 1 1 88 92 88 92
CHI ATL 2 1 75 88 180 88
HOU SEA 1 1 105 95 105 95
TOR DET 1 2 99 97 99 187
DAL STL 1 1 110 100 110 100
STL HOU 2 2 85 74 185 179
MIA CHI 1 3 95 98 95 265
SEA CHI 2 4 100 110 195 375