仅在
df
赛季 |
| 团队团队_Sason_Code | >TSL对手操作系统|||||
---|---|---|---|---|---|---|---|
2019 | 20181109 | >Abilene_Chr | <1-2019>94 | 主页阿肯色州_St | 15_2019<2td>73|||
2019 | 20181115 | >Abilene_Chr | <1-2019>客场 | 丹佛 | 82_2019>61 | ||
2019 | 20181122 | >Abilene_Chr | <1-2019>N | Elon | 70_2019>56 | [/tr>||
2019 | 20181123 | >Abilene_Chr | <1-2019>73远离太平洋 | 224_2019 | >71 | ||
2019 | 20181124 | >Abilene_Chr | <1-2019>N | UC_Riverside | 306_2019
您可以在重新设计位Overall_Season_Avg
:后使用merge
df.merge(Overall_Season_Avg
.set_index(['Team_Season_Code', 'Team'])
[['OS', 'TS']].add_prefix('O'),
left_on=['Opponent_Season_Code', 'Opponent'],
right_index=True, how='left'
)
输出:
Season Date Team Team_Season_Code TS L Opponent Opponent_Season_Code OS OOS OTS
0 2019 20181109 Abilene_Chr 1_2019 94 Home Arkansas_St 15_2019 73 65.242424 70.909091
1 2019 20181115 Abilene_Chr 1_2019 67 Away Denver 82_2019 61 72.156250 74.031250
2 2019 20181122 Abilene_Chr 1_2019 72 N Elon 70_2019 56 71.818182 73.636364
3 2019 20181123 Abilene_Chr 1_2019 73 Away Pacific 224_2019 71 76.466667 78.333333
4 2019 20181124 Abilene_Chr 1_2019 60 N UC_Riverside 306_2019 48 78.060606 79.545455
仅在Opponent_Season_Code
/Team_Season_Code
上合并:
df.merge(Overall_Season_Avg
.set_index('Team_Season_Code')
[['OS', 'TS']].add_prefix('O'),
left_on=['Opponent_Season_Code'],
right_index=True, how='left'
)
输出:
Season Date Team Team_Season_Code TS L Opponent Opponent_Season_Code OS OOS OTS
0 2019 20181109 Abilene_Chr 1_2019 94 Home Arkansas_St 15_2019 73 65.242424 70.909091
1 2019 20181115 Abilene_Chr 1_2019 67 Away Denver 82_2019 61 72.156250 74.031250
2 2019 20181122 Abilene_Chr 1_2019 72 N Elon 70_2019 56 71.818182 73.636364
3 2019 20181123 Abilene_Chr 1_2019 73 Away Pacific 224_2019 71 76.466667 78.333333
4 2019 20181124 Abilene_Chr 1_2019 60 N UC_Riverside 306_2019 48 78.060606 79.545455
df.merge(Overall_Season_Avg, on=['Team_Season_Code', 'Team'], how='left')
并将列的名称重命名为
或者在进行Overall_Season_Avg时使用transform
而不是agg
。
但我不保留转换代码,因为你没有提供可复制的示例
制作简单且可重复的plz-示例
https://stackoverflow.com/help/minimal-reproducible-example