このようなパンダのデータフレームがあります。
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