Sorting a DataFrame based on column names or row index labels using Dataframe.sort_index() in Python
In this article we will discuss how we organize the content of data entered based on column names or line reference labels using Dataframe.sort_index ().
Dataframe.sort_index():
In the Python Pandas Library, the Dataframe section provides a member sort sort_index () to edit DataFrame based on label names next to the axis i.e.
DataFrame.sort_index(axis=0, level=None, ascending=True, inplace=False, kind='quicksort', na_position='last', sort_remaining=True, by=None)
Where,
- axis :If the axis is 0, then the data name will be sorted based on the line index labels. The default is 0
- ascending :If the axis is 1, then the data name will be sorted based on column names.
- inplace : If the type of truth in the rise of another type arrange in order. The default is true
- na_position : If True, enable localization in Dataframe. Determines NaN status after filter i.e. irst puts NaNs first, finally puts NaNs at the end.
It returns the edited data object. Also, if the location dispute is untrue then it will return a duplicate copy of the provided data, instead of replacing the original Dataframe. While, if the internal dispute is true it will cause the current file name to be edited.
Let’s understand some examples,
# Program : import pandas as pd # List of Tuples students = [ ('Rama', 31, 'canada') , ('Symon', 23, 'Chennai' ) , ('Arati', 16, 'Maharastra') , ('Bhabani', 32, 'Kolkata' ) , ('Modi', 33, 'Uttarpradesh' ) , ('Heeron', 39, 'Hyderabad' ) ] # Create a DataFrame object dfObj = pd.DataFrame(students, columns=['Name', 'Marks', 'City'], index=['b', 'a', 'f', 'e', 'd', 'c']) print(dfObj)
Output : Name Marks City b Rama 31 canada a Symon 23 Chennai f Arati 16 Maharastra e Bhabani 32 Kolkata d Modi 33 Uttarpradesh c Heeron 39 Hyderabad
- Pandas : Convert Dataframe column into an index using set_index() in Python
- Pandas Dataframe: Get minimum values in rows or columns & their index position
- Python Pandas : Replace or change Column & Row index names in DataFrame
Now let’s see how we organize this DataFrame based on labels i.e. columns or line reference labels,
Sort rows of a Dataframe based on Row index labels :
Sorting by line index labels we can call sort_index()
in the data name item.
import pandas as pd # The List of Tuples students = [ ('Rama', 31, 'canada') , ('Symon', 23, 'Chennai' ) , ('Arati', 16, 'Maharastra') , ('Bhabani', 32, 'Kolkata' ) , ('Modi', 33, 'Uttarpradesh' ) , ('Heeron', 39, 'Hyderabad' ) ] # To create DataFrame object dfObj = pd.DataFrame(students, columns=['Name', 'Marks', 'City'], index=['b', 'a', 'f', 'e', 'd', 'c']) # By sorting the rows of dataframe based on row index label names modDFObj = dfObj.sort_index() print(' Dataframes are in sorted oreder of index value given:') print(modDFObj)
Output : Dataframes are in sorted oreder of index value given: Name Marks City a Symon 23 Chennai b Rama 31 canada c Heeron 39 Hyderabad d Modi 33 Uttarpradesh e Bhabani 32 Kolkata f Arati 16 Maharastra
As we can see in the output lines it is sorted based on the reference labels now. Instead of changing the original name data backed up an edited copy of the dataframe.
Sort rows of a Dataframe in Descending Order based on Row index labels :
Sorting based on line index labels in descending order we need to pass the argument = False
in sort_index()
function in the data object object.
import pandas as pd # The List of Tuples students = [ ('Rama', 31, 'canada') , ('Symon', 23, 'Chennai' ) , ('Arati', 16, 'Maharastra') , ('Bhabani', 32, 'Kolkata' ) , ('Modi', 33, 'Uttarpradesh' ) , ('Heeron', 39, 'Hyderabad' ) ] # To create DataFrame object dfObj = pd.DataFrame(students, columns=['Name', 'Marks', 'City'], index=['b', 'a', 'f', 'e', 'd', 'c']) # By sorting the rows of dataframe in descending order based on row index label names conObj = dfObj.sort_index(ascending=False) print('The Contents of Dataframe are sorted in descending Order based on Row Index Labels are of :') print(conObj)
The Contents of Dataframe are sorted in descending Order based on Row Index Labels are of : Name Marks City f Arati 16 Maharastra e Bhabani 32 Kolkata d Modi 33 Uttarpradesh c Heeron 39 Hyderabad b Rama 31 canada a Symon 23 Chennai
As we can see in the output lines it is sorted by destructive sequence based on the current reference labels. Also, instead of changing the original data name it restored the edited copy of the data.
Sort rows of a Dataframe based on Row index labels in Place :
Filtering a local data name instead of finding the default copy transfer inplace = True
in sort_index ()
function in the data object object to filter the data name with local reference label labels i.e.
import pandas as pd # The List of Tuples students = [ ('Rama', 31, 'canada') , ('Symon', 23, 'Chennai' ) , ('Arati', 16, 'Maharastra') , ('Bhabani', 32, 'Kolkata' ) , ('Modi', 33, 'Uttarpradesh' ) , ('Heeron', 39, 'Hyderabad' ) ] # To create DataFrame object dfObj = pd.DataFrame(students, columns=['Name', 'Marks', 'City'], index=['b', 'a', 'f', 'e', 'd', 'c']) #By sorting the rows of dataframe in Place based on row index label names dfObj.sort_index(inplace=True) print('The Contents of Dataframe are sorted in Place based on Row Index Labels are of :') print(dfObj)
Output : The Contents of Dataframe are sorted in Place based on Row Index Labels are of : Name Marks City a Symon 23 Chennai b Rama 31 canada c Heeron 39 Hyderabad d Modi 33 Uttarpradesh e Bhabani 32 Kolkata f Arati 16 Maharastra
Sort Columns of a Dataframe based on Column Names :
To edit DataFrame based on column names we can say sort_index () in a DataFrame object with an axis= 1 i.e.
import pandas as pd # The List of Tuples students = [ ('Rama', 31, 'canada') , ('Symon', 23, 'Chennai' ) , ('Arati', 16, 'Maharastra') , ('Bhabani', 32, 'Kolkata' ) , ('Modi', 33, 'Uttarpradesh' ) , ('Heeron', 39, 'Hyderabad' ) ] # To create DataFrame object dfObj = pd.DataFrame(students, columns=['Name', 'Marks', 'City'], index=['b', 'a', 'f', 'e', 'd', 'c']) # By sorting a dataframe based on column names conObj = dfObj.sort_index(axis=1) print('The Contents are of Dataframe sorted based on Column Names are in the type :') print(conObj)
Output : The Contents are of Dataframe sorted based on Column Names are in the type : City Marks Name b canada 31 Rama a Chennai 23 Symon f Maharastra 16 Arati e Kolkata 32 Bhabani d Uttarpradesh 33 Modi c Hyderabad 39 Heeron
As we can see, instead of changing the original data name it returns a fixed copy of the data data based on the column names.
Sort Columns of a Dataframe in Descending Order based on Column Names :
By sorting DataFrame based on column names in descending order, we can call sort_index ()
in the DataFrame item with axis = 1 and ascending = False i.e.
import pandas as pd # The List of Tuples students = [ ('Rama', 31, 'canada') , ('Symon', 23, 'Chennai' ) , ('Arati', 16, 'Maharastra') , ('Bhabani', 32, 'Kolkata' ) , ('Modi', 33, 'Uttarpradesh' ) , ('Heeron', 39, 'Hyderabad' ) ] # To create DataFrame object dfObj = pd.DataFrame(students, columns=['Name', 'Marks', 'City'], index=['b', 'a', 'f', 'e', 'd', 'c']) # By sorting a dataframe in descending order based on column names conObj = dfObj.sort_index(ascending=False, axis=1) print('The Contents of Dataframe sorted in Descending Order based on Column Names are of :') print(conObj)
Output : The Contents of Dataframe sorted in Descending Order based on Column Names are of : Name Marks City b Rama 31 canada a Symon 23 Chennai f Arati 16 Maharastra e Bhabani 32 Kolkata d Modi 33 Uttarpradesh c Heeron 39 Hyderabad
Instead of changing the original data name restore the edited copy of the data based on the column names (sorted by order)
Sort Columns of a Dataframe in Place based on Column Names :
Editing a local data name instead of obtaining an approved copy pass input = True and axis = 1 in sort_index () function in the dataframe object to filter the local data name by column names i.e.
import pandas as pd # The List of Tuples students = [ ('Rama', 31, 'canada') , ('Symon', 23, 'Chennai' ) , ('Arati', 16, 'Maharastra') , ('Bhabani', 32, 'Kolkata' ) , ('Modi', 33, 'Uttarpradesh' ) , ('Heeron', 39, 'Hyderabad' ) ] # To create DataFrame object dfObj = pd.DataFrame(students, columns=['Name', 'Marks', 'City'], index=['b', 'a', 'f', 'e', 'd', 'c']) # By sorting a dataframe in place based on column names dfObj.sort_index(inplace=True, axis=1) print('The Contents of Dataframe sorted in Place based on Column Names are of:') print(dfObj)
Output : The Contents of Dataframe sorted in Place based on Column Names are of: City Marks Name b canada 31 Rama a Chennai 23 Symon f Maharastra 16 Arati e Kolkata 32 Bhabani d Uttarpradesh 33 Modi c Hyderabad 39 Heeron
Want to expert in the python programming language? Exploring Python Data Analysis using Pandas tutorial changes your knowledge from basic to advance level in python concepts.
Read more Articles on Python Data Analysis Using Padas – Modify a Dataframe
- pandas.apply(): Apply a function to each row/column in Dataframe
- Pandas: Sort rows or columns in Dataframe based on values using Dataframe.sort_values()
- Apply a function to single or selected columns or rows in Dataframe
- Change data type of single or multiple columns of Dataframe in Python
- Change Column & Row names in DataFrame
- Convert Dataframe column type from string to date time
- Convert Dataframe column into to the Index of Dataframe
- Convert Dataframe indexes into columns