Skip to content

Sorting Rows in Python

[

pandas Sort: Your Guide to Sorting Data in Python

Learning pandas sort methods is a great way to start with or practice doing basic data analysis using Python. Most commonly, data analysis is done with spreadsheets, SQL, or pandas. One of the great things about using pandas is that it can handle a large amount of data and offers highly performant data manipulation capabilities.

In this tutorial, you’ll learn how to use .sort_values() and .sort_index(), which will enable you to sort data efficiently in a DataFrame.

By the end of this tutorial, you’ll know how to:

  • Sort a pandas DataFrame by the values of one or more columns
  • Use the ascending parameter to change the sort order
  • Sort a DataFrame by its index using .sort_index()
  • Organize missing data while sorting values
  • Sort a DataFrame in place using inplace set to True

To follow along with this tutorial, you’ll need a basic understanding of pandas DataFrames and some familiarity with reading in data from files.

Getting Started With Pandas Sort Methods

As a quick reminder, a DataFrame is a two-dimensional labeled data structure with columns of potentially different types. It’s similar to a spreadsheet or SQL table, but with more powerful indexing capabilities.

Here are the main steps we’ll cover in this tutorial:

  1. Preparing the Dataset
  2. Getting Familiar With .sort_values()
  3. Getting Familiar With .sort_index()

Preparing the Dataset

In this tutorial, we’ll be using a sample dataset to demonstrate the various sorting methods. The dataset contains information about students, including their names, ages, and grades.

import pandas as pd
data = {
'Name': ['Alice', 'Bob', 'Charlie', 'David'],
'Age': [18, 20, 19, 22],
'Grade': [85, 90, 92, 88]
}
df = pd.DataFrame(data)
print(df)

The output will be:

Name Age Grade
0 Alice 18 85
1 Bob 20 90
2 Charlie 19 92
3 David 22 88

This is our starting DataFrame. It includes four columns: Name, Age, and Grade. We’ll use this dataset for all the examples in this tutorial.

Getting Familiar With .sort_values()

The .sort_values() method is used to sort a DataFrame by the values of one or more columns. By default, the sort is performed in ascending order.

Let’s start by sorting the DataFrame by the Age column in ascending order:

sorted_df = df.sort_values('Age')
print(sorted_df)

The output will be:

Name Age Grade
0 Alice 18 85
2 Charlie 19 92
1 Bob 20 90
3 David 22 88

As you can see, the DataFrame is now sorted based on the values in the Age column. The rows are rearranged in ascending order of Age.

To sort the DataFrame in descending order, you can set the ascending parameter to False:

sorted_df = df.sort_values('Age', ascending=False)
print(sorted_df)

The output will be:

Name Age Grade
3 David 22 88
1 Bob 20 90
2 Charlie 19 92
0 Alice 18 85

Now the DataFrame is sorted in descending order based on the values in the Age column.

Getting Familiar With .sort_index()

The .sort_index() method is used to sort a DataFrame by its index. By default, the sort is performed in ascending order.

Let’s sort the DataFrame by the index in ascending order:

sorted_df = df.sort_index()
print(sorted_df)

The output will be the same as the original DataFrame:

Name Age Grade
0 Alice 18 85
1 Bob 20 90
2 Charlie 19 92
3 David 22 88

To sort the DataFrame by the index in descending order, you can set the ascending parameter to False:

sorted_df = df.sort_index(ascending=False)
print(sorted_df)

The output will be:

Name Age Grade
3 David 22 88
2 Charlie 19 92
1 Bob 20 90
0 Alice 18 85

Now the DataFrame is sorted by the index in descending order.

Conclusion

In this tutorial, you learned how to use .sort_values() and .sort_index() to sort data in a pandas DataFrame. You now have the knowledge and tools to sort your data efficiently and organize it in a way that suits your needs. Sorting data is an essential step in the data analysis process, and pandas provides powerful methods to make this process easy and intuitive. Keep practicing with different datasets to enhance your understanding and proficiency in sorting data with pandas.