5

There must be an obvious answer but I could neither find it in the sort_values()'s docs nor in related questions posts*

by in df.sort_values() accept columns labels, but How do I sort using the columns' position ?

I came up with this cumbersome code

df.iloc[df.iloc[:,1].sort_values().index]

to sort on the second column.

I was thinking that something like df.sort_values(by=1,axis=1) would exist. So what is syntacticly simple and correct way to do that ?


*Related questions: 1, 2, 3, 4, 5, 5, 6

2
  • Could you provide example data? Commented Nov 5, 2017 at 6:32
  • I'm not sure to understand. df.iloc[:, np.argsort(df.columns.values)] will reorder the columns in the df, right ? What I need is to sort the df on, for example, the 33th column's values. Commented Nov 5, 2017 at 6:34

1 Answer 1

4

Simpliest is select columns names by position:

df = df.sort_values(df.columns[1])
Sign up to request clarification or add additional context in comments.

2 Comments

yes ! I see. No shorter hand then ? Why not allowing positional int as a by value ?
In my opinion it is not implemented yet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.