How to Sort a Pandas DataFrame by Both Index and Column?

23 Dec, 2021
In this article, we will discuss how to sort a Pandas dataframe by both index and columns.
We can sort a Pandas DataFrame based on Index and column using sort_index method.
To sort the DataFrame based on the index we need to pass axis=0 as a parameter to sort_index method.
To sort the DataFrame based on the column name then we need to pass axis=1 as a parameter to sort_index method.
Syntax
DataFrame_Name.sort_index(axis=0, ascending=True, inplace=False, kind=’quicksort’)
Parameters
axis- Specifies on which basis to sort whether based on index or column. By default, it sorts based on index i.e. axis=0.
ascending- Specifies on which order to sort whether in ascending or descending order. It accepts True or False. By default it is True.
inplace- It specifies that the changes to the DataFrame is Temporary or Permanent. inplace=False indicates temporary and True indicates permanent.
kind- Specifies which sorting algorithm to use. It accepts quicksort, mergesort, heapsort, etc. By default, it is quicksort if not mentioned.
DataFrame
 
col2
col1
3
150
70
2
170
55
1
160
60
This was the DataFrame we are using in the below codes for sorting. By specifying axis=0 in sort_index method we can sort the DataFrame. Even if we don’t specify axis parameter in sort_index  by default it sorts the DataFrame based on row.
Example:
 In this example, the DataFrame is a sorted DataFrame based on index labels and this was temporarily sorted in python language.
Python3
# import necessary packagesimport pandas as pd # create 2 dataframes with different indexeshostelCandidates1 = pd.DataFrame({'col2': [150, 170, 160],                                  'col1': [70, 55, 60]},                                 index=[3, 2, 1])print('original DataFrame')print(hostelCandidates1) # sorted temperorle based on index labelsprint('Sorted by index')hostelCandidates1.sort_index(axis=0)
Output
original DataFrame
   col2  col1
3   150    70
2   170    55
1   160    60
Sorted by index

    col2     col1
1     160     60
2     170     55
3     150     70
To sort the data in the DataFrame on the basis of column names, then we need to pass axis=1 as a parameter to sort_index method in python language.
Python3
# import necessary packagesimport pandas as pd # create 2 dataframes with different indexeshostelCandidates1 = pd.DataFrame({'col2': [150, 170, 160],                                  'col1': [70, 55, 60]},                                 index=[3, 2, 1])print('original DataFrame')print(hostelCandidates1) # sorted temperorle based on column labelsprint('Sorted by column name')hostelCandidates1.sort_index(axis=1)
Output
original DataFrame
   col2  col1
3   150    70
2   170    55
1   160    60
Sorted by column name

    col1     col2
3     70     150
2     55     170
1     60     160
Example Code to sort the DataFrame & save changes permanently on original DataFrame:
Here the sort operation is performed directly on the original DataFrame and changes are saved permanently because of inplace=True argument.
Python3
# import necessary packagesimport pandas as pd # create 2 dataframes with different indexeshostelCandidates1 = pd.DataFrame({'col2': [150, 170, 160],                                  'col1': [70, 55, 60]},                                 index=[3, 2, 1])print('original DataFrame')print(hostelCandidates1) # sorted permanently based on column labelshostelCandidates1.sort_index(axis=1, inplace=True) print('Modified Original DataFrame')print(hostelCandidates1)
Output
original DataFrame
   col2  col1
3   150    70
2   170    55
1   160    60
Modified Original DataFrame
   col1  col2
3    70   150
2    55   170
1    60   160
simmytarika5
pandas-dataframe-program
Picked
Python pandas-dataFrame
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Python Classes and Objects
Python OOPs Concepts
Introduction To PYTHON
Python | os.path.join() method
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | Get unique values from a list
Python | datetime.timedelta() function