Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
wb_sunny

Sorting a Dataframe in Python – Step-by-Step

Sorting A Python DataFrame

Hey, readers! In this article, we will be focusing on Sorting a DataFrame in Python in detail. So, let us get started!


Sorting a DataFrame using sort_values() function

Python Pandas module provides us with various functions to deal with large data records. While dealing with the data records in terms of dataframes, we often come across situations wherein we need to sort the data and represent the output.

This is when, Python pandas.dataframe.sort_values() function comes into picture.

The sort_values() function sorts the data in ascending or descending order in a customized manner.

Let us now focus on the structure of the function in the upcoming section.


Syntax of sort_values() function in Python

Have a look at the below syntax!

pandas.DataFrame.sort_values(by, axis=0, ascending=True, kind=’mergesort’)
  • by: It represents the list of columns to be sorted.
  • axis: 0 represents row-wise sorting and 1 represents column-wise sorting.
  • ascending: If True, sorts the dataframe in ascending order.
  • kind: It can have three values: ‘Quicksort, mergesort or heapsort‘.

Let us now focus on the implementation of the sort_values() function in the upcoming section.


Example Code for Sorting a Dataframe in Python

In this example, we have initially created a data frame with pandas.dataframe() function. Further, we have sorted the column ‘RATE’ using the sort_values() function in descending order.

Example:

import pandas as pd
data = pd.DataFrame([[3,0,1], [4,4,4], [1,7,7], [10,10,10]],
     index=['Python', 'Java', 'C','Kotlin'],
     columns=['RATE','EE','AA'])

sort = data.sort_values("RATE", axis = 0, ascending = False)

print("Data before sorting:\n")
print(data)

print("Data after sorting:\n")
print(sort)

Output:

Data before sorting:

        RATE  EE  AA
Python     3   0   1
Java       4   4   4
C          1   7   7
Kotlin    10  10  10
Data after sorting:

        RATE  EE  AA
Kotlin    10  10  10
Java       4   4   4
Python     3   0   1
C          1   7   7

In the below example, we have sorted the above dataframe by two columns altogether -‘EE’ and ‘AA’ as shown below.

Example:

import pandas as pd
data = pd.DataFrame([[3,0,1], [4,4,4], [1,7,7], [10,10,10]],
     index=['Python', 'Java', 'C','Kotlin'],
     columns=['RATE','EE','AA'])

sort = data.sort_values(["EE","AA"], axis = 0, ascending = True)

print("Data before sorting:\n")
print(data)

print("Data after sorting:\n")
print(sort)

Output:

As visible below, the data frame gets sorted by the column ‘EE’ and ‘AA’ respectively in an ascending order.

Data before sorting:

        RATE  EE  AA
Python     3   0   1
Java       4   4   4
C          1   7   7
Kotlin    10  10  10
Data after sorting:

        RATE  EE  AA
Python     3   0   1
Java       4   4   4
C          1   7   7
Kotlin    10  10  10

Conclusion

By this, we have come to the end of this topic. We have understood the functioning of sort_values() function to sort the data frames.

Feel free to comment below, in case you come across any question. For more such posts related to Python, Stay tuned and Keep Learning!


References