给定一个带有一个描述性列和X个数字列的数据框,对于每一行,我想识别具有较高值的前N列,并将其保存为新数据帧上的行.
例如,请考虑以下数据框:
df = pd.DataFrame() df['index'] = ['A', 'B', 'C', 'D','E', 'F'] df['option1'] = [1,5,3,7,9,3] df['option2'] = [8,4,5,6,9,2] df['option3'] = [9,9,1,3,9,5] df['option4'] = [3,8,3,5,7,0] df['option5'] = [2,3,4,9,4,2]
我想输出(假设N是3,所以我想要前三名):
A,option3 A,option2 A,option4 B,option3 B,option4 B,option1 C,option2 C,option5 C,option4 (or option1 - ties arent really a problem) D,option5 D,option1 D,option2 and so on....
任何想法如何轻松实现?谢谢