我会这样做pd.wide_to_long
:
df['id'] = df.index ndf = pd.wide_to_long(df, ["Name", "Scr"], i="id", j="number").reset_index(0).set_index('Name') # id Scr # Name # NY 0 21 # AZ 1 31 # CA 0 45 # BK 1 46 # SF 0 37 # AK 1 23 # Thank you @jezrael for the improvement ndf.groupby('id')['Scr'].agg(['max','idxmax']).rename(columns= {'max':'Hi_Scr','idxmax':'Name'}) Name Hi Scr id 0 CA 45 1 BK 46