2

I'm trying to sort data by two columns. One of them by absolute value. It is easy to sort values by two columns http://pandas.pydata.org/pandas-docs/version/0.17/generated/pandas.DataFrame.sort_values.html and by abs value Sort by absolute value for one column, but I can't merge both approaches.

for instance, I have df that already ordered by abs 'dist' and then I want to sort it internally by 'taking'

In[4]:df
Out[4]: 
        q_id  dist  taking
    0   406   6.0    0.17
    1   448   6.0    0.46
    2   449   6.0    0.42
    3   208  -6.0    0.25
    4   244  -7.0    0.12
    5   203   7.0    0.40
    6   614   8.0    0.50
    7   243  -8.0    0.40

it may look like this

df_sorted
Out[]: 
        q_id  dist  taking
    1   448   6.0    0.46
    2   449   6.0    0.42
    3   208  -6.0    0.25
    0   406   6.0    0.17
    4   244  -7.0    0.12
    5   203   7.0    0.40
    6   614   8.0    0.50
    7   243  -8.0    0.40

Ascending=[True, False] is also required but I hope it shouldn't be a problem.

Does anyone know how to sort that DataFrame?

1 Answers1

4

Solution with helper column:

df = (df.assign(A=df['dist'].abs())
        .sort_values(['A','taking'],ascending=[True, False])
        .drop('A', 1))
print (df)
   q_id  dist  taking
1   448   6.0    0.46
2   449   6.0    0.42
3   208  -6.0    0.25
0   406   6.0    0.17
5   203   7.0    0.40
4   244  -7.0    0.12
6   614   8.0    0.50
7   243  -8.0    0.40
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252