答案 0 :(得分:0)
您可以使用np.sort
来实现您的目标:
In [4]:
df = pd.DataFrame(np.random.randn(5,3))
df
Out[4]:
0 1 2
0 -0.501631 0.729800 0.457293
1 0.259507 1.090150 -0.688817
2 0.611032 2.504964 -0.600550
3 -0.432279 -1.087437 -0.552484
4 -1.046578 -0.804681 0.477211
调用np.sort
,然后对数组进行切片:
In [37]:
np.sort(df)[:,-2:]
Out[37]:
array([[ 0.45729257, 0.7297998 ],
[ 0.25950659, 1.09014974],
[ 0.61103167, 2.50496433],
[-0.55248373, -0.43227871],
[-0.80468078, 0.47721114]])
如果要按降序排列值,请使用切片表示法反转数组:
In [34]:
np.sort(df)[:,::-1][:,:2]
Out[34]:
array([[ 0.7297998 , 0.45729257],
[ 1.09014974, 0.25950659],
[ 2.50496433, 0.61103167],
[-0.43227871, -0.55248373],
[ 0.47721114, -0.80468078]])