Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k points)

I have a dataset with multi-index columns in a pandas df that I would like to sort by values in a specific column. I have tried using sortindex and sortlevel but haven't been able get the results I am looking for. My dataset looks like:

    Group1    Group2

    A B C     A B C

1   1 0 3     2 5 7

2   5 6 9     1 0 0

3   7 0 2     0 3 5 

I want to sort all data and the index by column C in Group 1 in descending order so my results look like:

    Group1    Group2

    A B C     A B C

 2  5 6 9     1 0 0

 1  1 0 3     2 5 7

 3  7 0 2     0 3 5 

Is it possible to do this sort with the structure that my data is in, or should I be swapping Group1 to the index side?

1 Answer

0 votes
by (41.4k points)

When sorting by a MultiIndex you need to contain the tuple describing the column inside a list*:

In [11]: df.sort_values([('Group1', 'C')], ascending=False)

Out[11]: 

  Group1       Group2      

       A  B  C      A  B  C

2      5  6  9      1  0  0

1      1  0  3      2  5  7

3      7  0  2      0  3  5

If you are interested to learn Pandas visit this Python Pandas Tutorial.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...