Python

Pandas Sort by Column

A python is an excellent tool for data processing, thanks to a thriving community of data-centric Python tools. Pandas is also one of those programs, and it greatly simplifies data importation and analysis. Sorting the data in python can be done in different ways. When a user wants to sort a particular dataset according to some specific column, the question arises. According to the supplied column, the sort_values() method in Pandas organizes a data set in ascending or descending direction. It differs from the sorted Python method in that it can’t sort a data set and can’t choose a specific column. Therefore, we have decided to write this article to simplify sorting with the sort_values() function. Let’s start.

Example 01:

Let’s get started with our first example of today’s article on sorting the data frames of pandas via the columns. For this, you need to add the panda’s support in the code with its object “pd” and import the pandas. After this, we have started the code with the initialization of a dictionary dic1 with mixed types of key pairs. Most of them are strings, but the last key contains the integer type list as its value. Now, this dictionary dic1 has been converted into pandas DataFrame to display it in the tabular form of data using the DataFrame() function. The resultant data frame will be saved to the variable “d”. The print function is here to display the original data frame on Spyder 3 console using the variable “d” in it. Now, we have been utilizing the sort_values() function through data frame “d” to sort it according to the ascending order of column “c3” from the data frame and save it to the variable d1. This d1 sorted data frame will be printed out in the Spyder 3 console with the help of the run button.

import pandas as pd
dic1 = { 'c1': ['John', 'William', 'Laila'], 'c2': ['Jack', 'Worth', 'Sky'], 'c3': [36, 50, 25] }
d = pd.DataFrame(dic1)
print("\n Original DataFrame:\n", d)
d1 = d.sort_values('c3')
print("\n Sorted by column 3: \n", d1)

After running this code, we have got the original data frame and then the sorted data frame according to the ascending order of column c3.

Let’s say you want to order or sort the data frame in descending order; you can do that with the sort_values() function. You just need to add the ascending=False within its parameters. So, we have tried the same code with this new update. Also, this time, we have been sorting the data frame according to the descending order of column c2 and displaying it on the console.

import pandas as pd
dic1 = { 'c1': ['John', 'William', 'Laila'], 'c2': ['Jack', 'Worth', 'Sky'], 'c3': [36, 50, 25] }
d = pd.DataFrame(dic1)
print("\n Original DataFrame:\n", d)
d1 = d.sort_values('c1', ascending=False)
print("\n Sorted in Descending Order of column 1: \n", d1)

After running the updated code, we have got the original frame displayed on the console. After that, the sorted data frame according to the descending order of column c3 has been displayed.

Example 02:

Let’s get started with another example to see the working of the sort_values() function of pandas. But, this example will be a little different from the above example. We will be sorting the data frame according to the two columns. So, let’s start this code with the panda’s library as “pd” import at the first line. The integer type dictionary dic1 has been defined and has string type keys. The dictionary has been again converted into a data frame using the pandas everlasting DataFrame() function and saved to the variable “d”. The print method will display the data frame “d” on the Spyder 3 console. Now, the data frame will be sorted using the “sort_values()” function, taking two column names, c1 and c2, i.e. keys. The sorting order has been decided as ascending=True. The print statement will display the updated and sorted data frame “d” on the python tool screen.

import pandas as pd
dic1 = { 'c1': [3, 5, 7, 9], 'c2': [1, 3, 6, 8], 'c3': [23, 18, 14, 9] }
d = pd.DataFrame(dic1)
print("\n Original DataFrame:\n", d)
d1 = d.sort_values(by=['c1', 'c2'], ascending=True)
print("\n Sorted in Descending Order of column 1 & 2: \n", d1)

After this code was completed, we executed it in Spyder 3 and got the below result sorted according to the ascending order of columns c1 and c2.

Example 03:

Let’s have a look at the last example of sort_values() function usage. This time, we have initialized a dictionary of two lists of different types, i.e. strings and numbers. The dictionary has been converted into a set of data frames with the help of pandas “DataFrame()” function. The data frame “d” has been printed out as it is. We have utilized the “sort_values()” function two times to sort the data frame according to the column “Age” and column “Name” separately at two different lines. Both the sorted data frames have been printed out with the print method.

import pandas as pd
dic1 = { 'Name': ['John', 'William', 'Laila', 'Bryan', 'Jees'], 'Age': [15, 10, 34, 19, 37] }
d = pd.DataFrame(dic1)
print("\n Original DataFrame:\n", d)
d1 = d.sort_values(by='Age', na_position='first')
print("\n Sorted in Ascending Order of column 'Age': \n", d1)
d1 = d.sort_values(by='Name', na_position='first')
print("\n Sorted in Ascending Order of column 'Name': \n", d1)

After executing this code, we have got the original data frame displayed first. After that, the sorted data frame according to the column “Age” has been displayed. Last, the data frame has been sorted according to the column “Name” and displayed below.

Conclusion:

This article has beautifully explained the working of panda’s “sort_values()” function to sort any data frame according to its different columns. We have seen how to sort with a single column for more than 1 column in Python. All examples can be implemented on any python tool.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content