怎么让pandas DataFrame按照某一列的绝对值从小到按排列?
2个回答
没有直接的函数可以用,只能先建一个辅助列,然后根据辅助列排序,然后再把辅助列删掉
代码如下
In [162]:
df['sort_helper'] = df['b'].abs()
df.sort_values('sort_helper').drop('sort_helper', axis=1)
Out[162]:
   a  b
2  3 -1
3  4  2
0  1 -3
1  2  5
4  5 -9比如按照‘a'列的绝对值从小到大排序
df.iloc[df['a'].abs().argsort()]按照绝对从大到小排序
df.iloc[df['a'].abs().argsort()[::-1]]SofaSofa数据科学社区DS面试题库 DS面经
				这个更简洁
				- 
             LiShanfei
           
				
          
          2019-07-29 00:20
			
    
  相关讨论
  随便看看