1

A simple example, let's say I have this DataFrame:

raw = pd.DataFrame(np.array([
    [1, 2, 5, 'User 1', 5],
    [1, 2, 3, 'User 2', 3],
    [1, 2, 2, 'User 3', 5],
    [3, 2, 4, 'User 4', 7],
    [3, 2, 3, 'User 5', 2],
    [3, 1, 6, 'User 6', 5],
    [2, 2, 6, 'User 7', 1],
    [2, 2, 5, 'User 8', 4],
    [2, 2, 3, 'User 9', 8],
]), columns=['a', 'b', 'c', 'd', 'e'])

and below that code:

raw.set_index(['a', 'b', 'c'], inplace=True)
raw.sort_index()
raw.head(9)

The output looks like that:

The output of the Dataframe

Instead of sorting it first by a, then by b, then by c. ( the whole row ) How could it be solved?

1 Answer 1

1

Add inplace=True parameter DataFrame.sort_index:

raw.sort_index(inplace=True)

Or assign output back:

raw = raw.sort_index()

print (raw)
            d  e
a b c           
1 2 2  User 3  5
    3  User 2  3
    5  User 1  5
2 2 3  User 9  8
    5  User 8  4
    6  User 7  1
3 1 6  User 6  5
  2 3  User 5  2
    4  User 4  7
Sign up to request clarification or add additional context in comments.

Comments

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.