Pandas library in Python gives different functionalities to work with the data in an efficient way. It also provides a Pandas dataframe that regulates the data in an organized manner. It can be thought of as an SQL table that has information stored inside its cell. It also provides with functionalities that can sort the data of the dataframe’s columns. 

This article discusses how a Pandas dataframe column can be sorted using the sort_values() method in Python. It also discusses how more than one column of the Pandas dataframe can be sorted.

How to sort pandas dataframe from one column?

A 2-dimensional structure that can save the data and is used for efficient data handling. Using the Pandas DataFrame, the data can not only be saved but also analyzed, modified, sorted, and filtered. The data in the Pandas DataFrame’s column can be sorted using the sort_values() method.

Let us discuss different methods to sort a single column in a Pandas dataframe.

Method 1: Sorting a Single Column of a Pandas DataFrame in an Ascending Order

Calling the sort_values method on the Pandas dataframe with the column name provided as a string parameter sorts the elements of that column in ascending order. The following code explains how sort_values() method can be used to sort the Pandas dataframe in ascending order:

import pandas as pd
book_info = [['The 7 habits of highly effective people', 1989, 'Stephen Covey'],
['Atomic habits', 2018, 'James Clear'],
['Tuesdays with Morries', 1997,'Mitch Albom'],
['Ikigai', 2016, 'Francesc Miralles Hector Garcia'],
['Murder of History', 1993, 'Khursheed Kamal Aziz'],
['Pride and prejudice', 1813, 'Jane Austen']]
dframe = pd.DataFrame(book_info, columns=['Name', 'Publishing Year',
'Author'])
print("The original dataframe is:\n",dframe)
dframe = dframe.sort_values('Publishing Year')
print("The modified dataframe is:\n",dframe)

In the above code,

  • The Pandas library is imported as pd.
  • The book_info is a nested list in which each sublist contains three elements that represent the information about the books.
  • The pd.DataFrame converts the list book_info Indo a Pandas DataFrame where the first element of each element represents the column Name, the second element of each sublist represents the column Publishing Year, and the third list of each column represents the column Author.
  • The original DataFrame is printed.
  • The function sort_values is called on dframe where the values of the column will be sorted in ascending order. 
  • The modified DataFrame is printed.

Output

The following output displays how the values in the column ‘Publishing Year’ have been sorted in ascending order:

Method 2: Sorting a Single Column of the Pandas DataFrame in Descending Order

If the column of the Pandas DataFrame needs to be sorted in descending order, the sort_values method will get the parameter value of ascending=False. This will sort the values of the column in the descending order. The following modifications will be made to the code above:

dframe = dframe.sort_values('Publishing Year',ascending=False )

Output

The following output displays how the values in the column ‘Publishing Year’ have been sorted in descending order:

Method 3: Sorting a Single Column of the Pandas DataFrame Using Custom Functions

A column in a Pandas DataFrame can also be sorted using a custom function. The sort_values() function is called on the DataFrame dframe where the by parameter equates to ‘Name’. It suggests that the ‘Name’ column will be sorted. The key parameter equates to the Lambda function that applies str.lower() method to each element in the ‘Name’ column. The following line of code explains how we can sort a column in a Pandas DataFrame:

dframe= dframe.sort_values(by='Name', key=lambda col: col.str.lower())

Output

The following output displays how a column in a Pandas DataFrame can be sorted using the custom Lambda function:

How to Sort Pandas DataFrame for Multiple Columns?

The data in multiple columns of the Pandas DataFrame can be sorted by giving a list containing the column names by the parameter of the sorted_values method. In this example, the values in the list are ‘Name’ and ‘Publishing Year’ which means that these columns will be sorted. The following code explains how to sort multiple columns by changing the values in by parameter:

dframe = dframe.sort_values(by=['Name','Publishing Year'])

Output

The following output displays how the sort_values() method can be used to sort multiple columns in the Pandas DataFrame:

Conclusion

Sorting is an integral part of the data analysis process and the elements inside a column in a Pandas dataframe can be sorted using the sort_values method. The data can be sorted in ascending and descending order while the data can also sorted according to one’s own requirements using the custom function. The data of multiple columns can also be sorted using the sort_values method. The article discusses different methods to sort the Pandas dataframe from one column and explains it with relevant examples.