In this article we will discuss how to sort the contents of dataframe based on column names or row index labels using Dataframe.sort_index().

Dataframe.sort_index()

In Python’s Pandas Library, Dataframe class provides a member function sort_index() to sort a DataFrame based on label names along the axis i.e.

Important arguments are,

  • axis : If axis is 0, then dataframe will sorted based on row index labels. Default is 0
    • If axis is 1, then dataframe will sorted based on column names.
  • ascending : If True sort in ascending else sort in descending order. Default is True
  • inplace : If True, perform operation in-place in Dataframe
  • na_position : Decides the position of NaNs after sorting i.e. irst puts NaNs at the beginning, last puts NaNs at the end
    Default value is ‘first’

It returns a sorted dataframe object. Also, if inplace argument is not True then it will return a sorted copy of given dataframe, instead of modifying the original Dataframe. Whereas, if inplace argument is True then it will make the current dataframe sorted.

Let’s understand by some examples,

First of all create a Dataframe object i.e.

Contents of the created dataframe are,

Now let’s see how to sort this DataFrame based on labels i.e. either column or row index labels,

Sort rows of a Dataframe based on Row index labels

To sort based on row index labels we can call sort_index() on the dataframe object

Output:

 

As we can see in the output rows are sorted based on the index labels now. Instead of modifying the original dataframe it returned a sorted copy of dataframe.

Sort rows of a Dataframe in Descending Order based on Row index labels

To sort based on row index labels in descending order we need to pass argument ascending=False in sort_index() function on the dataframe object,

Output:

As we can see in the output rows are sorted in descedning order based on the index labels now. Also, instead of modifying the original dataframe it returned a sorted copy of dataframe.

Sort rows of a Dataframe based on Row index labels in Place

To sort a dataframe inplace instead of getting a sorted copy pass argument inplace=True in sort_index() function on the dataframe object to sort the dataframe by row index labels inplace i.e.

Output:

As we can see in the output rows of the dataframe are sorted in place.

Sort Columns of a Dataframe based on Column Names

To sort a DataFrame based on column names we can call sort_index() on the DataFrame object with argument axis=1 i.e.

Output:

As we can see, instead of modifying the original dataframe it returned a sorted copy of dataframe based on column names.

Sort Columns of a Dataframe in Descending Order based on Column Names

To sort a DataFrame based on column names in descending Order, we can call sort_index() on the DataFrame object with argument axis=1 and ascending=False i.e.

Output:

Instead of modifying the original dataframe it returned a sorted copy of dataframe based on column names ( sorted in descending Order)

Sort Columns of a Dataframe in Place based on Column Names

To sort a dataframe inplace instead of getting a sorted copy pass arguments inplace=True and axis=1 in sort_index() function on the dataframe object to sort the dataframe inplace by column names i.e.

Output:

As we can see in the output rows of the dataframe are sorted in place.

For sorting based on contents of a Dataframe look at the following article,

Pandas: Sort rows or columns in Dataframe based on values using Dataframe.sort_values()

Complete example is as follows,

Output:

 

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Do Subscribe with us for more Articles / Tutorials like this,