2 回答
TA贡献1848条经验 获得超10个赞
您可以使用合并
teams = pd.DataFrame({"team": ["T0", "T1", "T2", "T3", "T4", "T5"],
"A": ["A0", "A1", "A2", "A3", "A4", "A5"]})
t1_stats = teams.add_prefix("T1_")
t2_stats = teams.add_prefix("T2_")
games = pd.DataFrame({"gameID": ["G0", "G1", "G2"],
"T1": [ "T0", "T2", "T4"],
"T2": [ "T1", "T5", "T3"],
"X": ["X0", "X1", "X2"]})
games_with_team_stats = games.merge(t1_stats, "left", left_on="T1", right_on="T1_team").merge(t2_stats, left_on="T2", right_on="T2_team")
合并的工作方式类似于 SQL 联接,但有一个主要缺点 - 在合并过程中无法重命名合并的数据帧的键。对于您来说,最简单的选择似乎是创建 2 个临时数据帧,这些数据帧具有用于合并的相应列名。
TA贡献1872条经验 获得超3个赞
提供的解决方案是正确的,因此请接受该解决方案,但您也可以在执行合并的行中添加前缀:
import pandas as pd
stats = pd.DataFrame([
['Atlanta Hawks', 108.8 , 93.4 , 15.4 , 44.7 , 1.13 , 13.0 , 44.4 , 61.7 , 53.5 , 16.5 , 47.1 , 49.7 , 91.00 , 66.2],
['Boston Celtics', 95.4 , 84.2 , 11.2 , 47.7 , 0.84 , 12.6 , 24.7 , 69.6 , 48.2, 19.0 , 45.9 , 51.9 , 97.75 , 60.1],
['Charlotte Bobcats', 90.8 , 111.6 , -20.8 , 64.0 , 1.23 , 13.6 , 32.1 , 64.1 , 45.7 , 14.9 , 35.8 , 45.1 , 86.50 , 31.2],
['Chicago Bulls', 96.9, 100.0 , -3.1 , 63.3 , 0.95 , 14.3 , 38.1 , 69.3 , 53.0 , 20.6 , 41.5 , 50.7 , 96.25 , 50.5],
['Cleveland Cavaliers', 100.6 , 92.9 , 7.7, 60.9 , 1.05 , 16.0 ,32.1 , 71.6, 54.1, 20.6 , 49.3 , 54.1 , 90.50 , 61.2]],
columns=['TEAM','OFFRTG','DEFRTG','NETRTG','AST%','AST/TO','ASTRATIO','OREB%','DREB%','REB%','TOV%','EFG%',
'TS%','PACE','PIE'])
matchups = pd.DataFrame([
['Cleveland Cavaliers', 85 , 'Boston Celtics' , 90, 0 , 0],
['Milwaukee Bucks', 95 , 'Chicago Bulls' , 108, 0 , 0],
['Portland Trail Blazers', 76 , 'Los Angeles Lakers' , 96 , 0 , 0],
['Toronto Raptors', 95 , 'Philadelphia 76ers', 84 , 1 , 0],
['New Jersey Nets', 95 , 'Washington Wizards' , 85 , 1 , 0 ]],
columns = ['Team1','Team1Score','Team2','Team2Score','Team1Win','Team1Home'])
df = matchups.merge(stats.add_prefix('Team1'), how='left', left_on=['Team1'], right_on=['Team1TEAM']).drop(['Team1TEAM'],axis=1).merge(stats.add_prefix('Team2'), how='left', left_on=['Team2'], right_on=['Team2TEAM']).drop(['Team2TEAM'],axis=1)
输出:
print(df.to_string())
Team1 Team1Score Team2 Team2Score Team1Win Team1Home Team1OFFRTG Team1DEFRTG Team1NETRTG Team1AST% Team1AST/TO Team1ASTRATIO Team1OREB% Team1DREB% Team1REB% Team1TOV% Team1EFG% Team1TS% Team1PACE Team1PIE Team2OFFRTG Team2DEFRTG Team2NETRTG Team2AST% Team2AST/TO Team2ASTRATIO Team2OREB% Team2DREB% Team2REB% Team2TOV% Team2EFG% Team2TS% Team2PACE Team2PIE
0 Cleveland Cavaliers 85 Boston Celtics 90 0 0 100.6 92.9 7.7 60.9 1.05 16.0 32.1 71.6 54.1 20.6 49.3 54.1 90.5 61.2 95.4 84.2 11.2 47.7 0.84 12.6 24.7 69.6 48.2 19.0 45.9 51.9 97.75 60.1
1 Milwaukee Bucks 95 Chicago Bulls 108 0 0 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 96.9 100.0 -3.1 63.3 0.95 14.3 38.1 69.3 53.0 20.6 41.5 50.7 96.25 50.5
2 Portland Trail Blazers 76 Los Angeles Lakers 96 0 0 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
3 Toronto Raptors 95 Philadelphia 76ers 84 1 0 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
4 New Jersey Nets 95 Washington Wizards 85 1 0 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
添加回答
举报