1 回答
TA贡献1777条经验 获得超10个赞
这本质上是按一列进行透视:
out_df = (df.assign(col=df.groupby('Name1').cumcount()+1)
.pivot_table(index='Name1', columns='col', aggfunc='first')
.swaplevel(0,1,axis=1)
.sort_index(axis=1)
)
out_df.columns = [f"{y}{x}" if y=="Score" else f"M{x}" for x,y in out_df.columns]
out_df = out_df.reset_index()
输出:
Name1 M1 Score1 M2 Score2 M3 Score3
-- ----------------- ------------------ -------- ------- -------- --------- --------
0 AIR PRODUCTS AIR PRODUCT 0.78 AIR PRO 0.63 PRODUCTS 0.39
1 ARDAGH GLASS ARDAGH 0.6 nan nan nan nan
2 BELFIUS INSURANCE BELFIUS INSURANCES 0.79 BELFIUS 0.5 BELFIUS T 0.31
添加回答
举报