I have the following dataframe:
id p1 p2 p3 p4 1 0 9 1 4 2 0 2 3 4 3 1 3 10 7 4 1 5 3 1 5 2 3 7 10
You can use:
df = df.set_index('id').apply(lambda x: pd.Series(x.sort_values(ascending=False) .iloc[:3].index, index=['top1','top2','top3']), axis=1).reset_index() print (df) id top1 top2 top3 0 1 p2 p4 p3 1 2 p4 p3 p2 2 3 p3 p4 p2 3 4 p2 p3 p4 4 5 p4 p3 p2