How to Sort a Pandas DataFrame by Both Index and Column?
Last Updated :
31 Jul, 2023
In this article, we will discuss how to sort a Pandas dataframe by both index and columns.
Sort DataFrame based on Index
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
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 pandas as pd
hostelCandidates1 = pd.DataFrame({ 'col2' : [ 150 , 170 , 160 ],
'col1' : [ 70 , 55 , 60 ]},
index = [ 3 , 2 , 1 ])
print ( 'original DataFrame' )
print (hostelCandidates1)
print ( '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
Sort DataFrame based on columns
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 pandas as pd
hostelCandidates1 = pd.DataFrame({ 'col2' : [ 150 , 170 , 160 ],
'col1' : [ 70 , 55 , 60 ]},
index = [ 3 , 2 , 1 ])
print ( 'original DataFrame' )
print (hostelCandidates1)
print ( '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 pandas as pd
hostelCandidates1 = pd.DataFrame({ 'col2' : [ 150 , 170 , 160 ],
'col1' : [ 70 , 55 , 60 ]},
index = [ 3 , 2 , 1 ])
print ( 'original DataFrame' )
print (hostelCandidates1)
hostelCandidates1.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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...