2 回答
TA贡献1840条经验 获得超5个赞
您可以使用ffill:
df['match'] = df['HPSame'].where(df['query'] != df['query'].shift()).ffill()
输出:
query subject HPSame match
0 WP_77.1 WP_706.1 HPS_1 HPS_1
1 WP_78.1 WP_46.1 HPS_2 HPS_2
2 WP_57.1 WP_26.1 HPS_3 HPS_3
3 WP_57.1 WP_627.1 HPS_4 HPS_3
4 WP_15.1 WP_16.1 HPS_5 HPS_5
5 WP_15.1 WP_17.1 HPS_6 HPS_5
6 WP_15.1 WP_63.1 HPS_7 HPS_5
7 WP_15.1 WP_61.1 HPS_8 HPS_5
8 WP_15.1 WP_56.1 HPS_9 HPS_5
9 WP_40.1 WP_11.1 HPS_10 HPS_10
TA贡献1856条经验 获得超11个赞
您也可以使用groupby.transform('first')如
df['match'] = (
df.groupby('query')['HPSame'].transform('first')
.reset_index(drop=True)
)
输出
query subject HPSame match
0 WP_77.1 WP_706.1 HPS_1 HPS_1
1 WP_78.1 WP_46.1 HPS_2 HPS_2
2 WP_57.1 WP_26.1 HPS_3 HPS_3
3 WP_57.1 WP_627.1 HPS_4 HPS_3
4 WP_15.1 WP_16.1 HPS_5 HPS_5
5 WP_15.1 WP_17.1 HPS_6 HPS_5
6 WP_15.1 WP_63.1 HPS_7 HPS_5
7 WP_15.1 WP_61.1 HPS_8 HPS_5
8 WP_15.1 WP_56.1 HPS_9 HPS_5
9 WP_40.1 WP_11.1 HPS_10 HPS_10
添加回答
举报