How to sort rows or columns in Dataframe based on values using Dataframe.sort_values() in Python ?
In this article we will get to know about how to sort rows in ascending & descending order based on values in columns and also sorting columns based on value in rows using member function DataFrame.sort_values()
.
DataFrame.sort_values() :
Pandas library contains a Dataframe class which provide a function to arrange the contents of dataframe.
Syntax : DataFrame.sort_values(by, axis=0, ascending=True, inplace=False, kind='quicksort', na_position='last')
where,
- by: Column names or index labels where sorting is to be done.
- axis: If axis=0. names by argument will be taken as column names and If axis=1, names by argument will be taken as row index labels
- ascending: If True: Ascending Sorting , Else: Descending Sorting
- inplace: When true in-place operation performed in dataframe
- na_position: If True: It will sort the current dataframe , Else: Return a sorted copy of dataframe instead of modifying the dataframe.
Sorting Dataframe rows based on a single column :
To sort rows in dataframe based on ‘Team’ column, we will pass ‘Team’ as argument.
#Program : import pandas as sc # List of Tuples as Players players = [ ('Curran', 325, 'CSK') , ('Ishan', 481, 'MI' ) , ('Vijay', 106, 'SRH') , ('Pant', 224, 'Delhi' ) , ('Seifert', 65, 'KKR' ) , ('Hooda', 440, 'PK' ) ] # To Create DataFrame object datafObjs = sc.DataFrame(players, columns=['Name', 'Runs', 'Team'], index=['i', 'ii', 'iii', 'iv', 'v', 'vi']) # Sorting the rows of dataframe by column 'Team' datafObjs = datafObjs.sort_values(by ='Team' ) print("Now the sorted Dataframe based on column 'Team' is : ") print(datafObjs)
Output : Now the sorted Dataframe based on column 'Team' is : Name Runs Team i Curran 325 CSK iv Pant 224 Delhi v Seifert 65 KKR ii Ishan 481 MI vi Hooda 440 PK iii Vijay 106 SRH
- Pandas : Sort a DataFrame based on column names or row index labels using Dataframe.sort_index()
- Pandas : Convert Dataframe column into an index using set_index() in Python
- Pandas Dataframe: Get minimum values in rows or columns & their index position
Sorting Dataframe rows based on multiple column :
To sort all rows of dataframe based on Team & Runs column.
#Program : import pandas as sc # List of Tuples as Players players = [ ('Curran', 325, 'CSK') , ('Ishan', 481, 'MI' ) , ('Vijay', 106, 'SRH') , ('Pant', 224, 'Delhi' ) , ('Seifert', 65, 'KKR' ) , ('Hooda', 440, 'PK' ) ] # To Create DataFrame object datafObjs = sc.DataFrame(players, columns=['Name', 'Runs', 'Team'], index=['i', 'ii', 'iii', 'iv', 'v', 'vi']) # Sorting the rows of dataframe by column 'Team' datafObjs = datafObjs.sort_values(by =['Runs', 'Team']) print("Now the sorted Dataframe on the basis of columns 'Name' & 'Marks' : ") print(datafObjs)
Output : Now the sorted Dataframe on the basis of columns 'Name' & 'Marks' : Name Runs Team v Seifert 65 KKR iii Vijay 106 SRH iv Pant 224 Delhi i Curran 325 CSK vi Hooda 440 PK ii Ishan 481 MI
Sorting Dataframe rows based on columns in Descending Order :
Here we will sort the dataframe in descending order by passing value as false in argument ascending.
#Program : import pandas as sc # List of Tuples as Players players = [ ('Curran', 325, 'CSK') , ('Ishan', 481, 'MI' ) , ('Vijay', 106, 'SRH') , ('Pant', 224, 'Delhi' ) , ('Seifert', 65, 'KKR' ) , ('Hooda', 440, 'PK' ) ] # To Create DataFrame object datafObjs = sc.DataFrame(players, columns=['Name', 'Runs', 'Team'], index=['i', 'ii', 'iii', 'iv', 'v', 'vi']) # Sortimg rows of dataframe by column 'Name' in descending manner datafObjs = datafObjs.sort_values(by ='Name' , ascending=False) print("Now the sorted Dataframe on basis of column 'Name' in Descending manner : ") print(datafObjs)
Output : Now the sorted Dataframe on basis of column 'Name' in Descending manner : Name Runs Team iii Vijay 106 SRH v Seifert 65 KKR iv Pant 224 Delhi ii Ishan 481 MI vi Hooda 440 PK i Curran 325 CSK
Sorting Dataframe rows based on a column in Place :
Here we will sort the dataframe based on single column in place with argument inplace
and value True
.
#Program : import pandas as sc # List of Tuples as Players players = [ ('Curran', 325, 'CSK') , ('Ishan', 481, 'MI' ) , ('Vijay', 106, 'SRH') , ('Pant', 224, 'Delhi' ) , ('Seifert', 65, 'KKR' ) , ('Hooda', 440, 'PK' ) ] # To Create DataFrame object datafObjs = sc.DataFrame(players, columns=['Name', 'Runs', 'Team'], index=['i', 'ii', 'iii', 'iv', 'v', 'vi']) # Sorting e rows of the dataframe by column 'Name' inplace datafObjs.sort_values(by='Team' , inplace=True) print("Now the sorted Dataframe based on a single column 'Team' inplace : ") print(datafObjs)
Output : Now the sorted Dataframe based on a single column 'Team' inplace : Name Runs Team i Curran 325 CSK iv Pant 224 Delhi v Seifert 65 KKR ii Ishan 481 MI vi Hooda 440 PK iii Vijay 106 SRH
Sorting columns of a Dataframe based on a single or multiple rows :
Let’s take a example where column of a dataframe is to be sorted on the basis of single or multiple rows.
#program : import pandas as sc # List of Tuples as matrix matrix = [(11, 2, 33), (4, 55, 6), (77, 8, 99), ] # To create a DataFrame object like a Matrix datafObjs = sc.DataFrame(matrix, index=list('abc')) print("Sorting the daataframe based on single rows or multiple rows: ") print(datafObjs)
Output : Sorting the daataframe based on single rows or multiple rows: 0 1 2 a 11 2 33 b 4 55 6 c 77 8 99
Sorting columns of a Dataframe based on a single row :
Now from above dataframe let’s sort the dataframe on basis of ‘b’ row.
#program : import pandas as sc # List of Tuples as matrix matrix = [(11, 2, 33), (4, 55, 6), (77, 8, 99), ] # To create a DataFrame object like a Matrix datafObjs = sc.DataFrame(matrix, index=list('abc')) datafObjs = datafObjs.sort_values(by ='b', axis=1) print("Now the sorted Dataframe on the basis of single row index label 'b' : ") print(datafObjs)
Output : Now the sorted dataframe is sorted on the basis of row index label 'b' : 0 2 1 a 11 33 2 b 4 6 55 c 77 99 8
Sorting columns of a Dataframe in Descending Order based on a single row :
Here we will sort columns of the dataframe in descending order based on single row.
Here we will sort columns of a Dataframe on basis of multiple rows by passing value in ascending as false.
#Program : import pandas as sc # List of Tuples as matrix matrix = [(11, 2, 33), (4, 55, 6), (77, 8, 99), ] # To create a DataFrame object like a Matrix datafObjs = sc.DataFrame(matrix, index=list('abc')) # Sorting the columns of a dataframe in descending order based on a single row with index label 'b' datafObjs = datafObjs.sort_values(by='b', axis=1, ascending=False) print("Now the sorted Dataframe on the basis of single row index label 'b' in descending order :") print(datafObjs)
Output : Now the sorted Dataframe on the basis of single row index label 'b' in descending order : 1 2 0 a 2 33 11 b 55 6 4 c 8 99 77
Sorting columns of a Dataframe based on a multiple rows :
Here the columns of dataframe are sorted on the basis of multiple rows with passing labels ‘b’ & ‘c’ and axis=1.
#program : import pandas as sc # List of Tuples as matrix matrix = [(11, 2, 33), (4, 55, 6), (77, 8, 99), ] # To create a DataFrame object like a Matrix datafObjs = sc.DataFrame(matrix, index=list('abc')) # Sorting columns of the dataframe based on a multiple row with index labels 'b' & 'c' datafObjs = datafObjs.sort_values(by =['b' , 'c' ], axis=1) print("Now the sorted Dataframe based on multiple rows index label 'b' & 'c' :") print(datafObjs)
Output : Now the sorted Dataframe based on multiple rows index label 'b' & 'c' : 0 2 1 a 11 33 2 b 4 6 55 c 77 99 8
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
- Apply a function to single or selected columns or rows in Dataframe
- Sort a DataFrame based on column names or row index labels using Dataframe.sort_index() in Pandas
- 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