Sorting by absolute value without changing the data

前端 未结 2 1376
无人共我
无人共我 2020-12-05 02:01

I\'m looking for a simple way to sort a pandas dataframe by the absolute value of a particular column, but without actually changing the values within the dataframe. Somethi

相关标签:
2条回答
  • 2020-12-05 02:32

    UPDATE

    Since 0.17.0 order and sort have been deprecated (thanks @Ruggero Turra), you can use sort_values to achieve this now:

    In[16]:
    
    df.reindex(df.b.abs().sort_values().index)
    Out[16]: 
       a  b
    2  3 -1
    3  4  2
    0  1 -3
    1  2  5
    4  5 -9
    
    0 讨论(0)
  • 2020-12-05 02:39

    Towards more idiomatic pandas: Use argsort

    A cleaner approach would be to call Series.argsort on the absolute values, and then index:

    df.iloc[df['b'].abs().argsort()]
    
       a  b
    2  3 -1
    3  4  2
    0  1 -3
    1  2  5
    4  5 -9
    

    If you need to reset the index, use Series.reset_index,

    df.iloc[df['b'].abs().argsort()].reset_index(drop=True)
    
       a  b
    0  3 -1
    1  4  2
    2  1 -3
    3  2  5
    4  5 -9
    

    Lastly, since argsort does not have an ascending parameter to specify ascending/descending order, you will need to negate df['b'].abs() to sort by descending order.

    df.iloc[(-df['b'].abs()).argsort()]
    
       a  b
    4  5 -9
    1  2  5
    0  1 -3
    3  4  2
    2  3 -1
    

    You can do this with NumPy as well—use np.abs and ndarray.argsort.

    df.iloc[np.abs(df['b'].values).argsort()]
    
       a  b
    2  3 -1
    3  4  2
    0  1 -3
    1  2  5
    4  5 -9
    

    Or, for descending order,

    df.iloc[(-np.abs(df['b'].values)).argsort()]
    
       a  b
    4  5 -9
    1  2  5
    0  1 -3
    3  4  2
    2  3 -1
    
    0 讨论(0)
提交回复
热议问题