如何在python中为vlookup创建代码



df

团队>TSL对手操作系统><1-2019>主页15_2019<2td>73><1-2019>82_2019>><1-2019>70_2019>[/tr>><1-2019>73远离>><1-2019>306_2019
赛季 日期 团队_Sason_Code
2019 20181109Abilene_Chr94阿肯色州_St
2019 20181115Abilene_Chr客场丹佛61
2019 20181122Abilene_ChrNElon56
2019 20181123Abilene_Chr太平洋224_201971
2019 20181124Abilene_ChrNUC_Riverside

您可以在重新设计位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

相关内容

  • 没有找到相关文章

最新更新