AttributeError: 'DataFrame' object has no attribute 'sort'

calendar_today agoschedule3 min read

Python is a powerful programming language that is widely used for data analysis, machine learning, and scientific computing. One of the most popular libraries in Python for data analysis is pandas. It provides a DataFrame object that allows you to work with data in a tabular format. And as a data scientist or data analyst, working with data is an everyday task, and it's common to encounter different errors while working with data. One of the most common errors is the AttributeError: 'DataFrame' object has no attribute 'sort' . This error occurs when we try to sort a DataFrame in Python using the sort() method. In this article, we will discuss in detail about the error, its causes, and solutions to fix it.

AttributeError: 'DataFrame' object has no attribute 'sort' #

The AttributeError: 'DataFrame' object has no attribute 'sort' error occurs when you try to use the sort() method on a DataFrame object. The sort() method is used to sort the rows of a DataFrame based on one or more columns. However, this method is not available in the DataFrame object, and hence the error is thrown.

For example, consider the following code:


import pandas as pd
data = {'name': ['John', 'Jane', 'Bob', 'Alice'],
        'age': [25, 30, 20, 40]
       }
df = pd.DataFrame(data)
df.sort('age')

If we try to sort the DataFrame based on the 'Age' column using the sort() method, we will get the following error:


AttributeError: 'DataFrame' object has no attribute 'sort'

Reasons that will cause this error #

There are several reasons that can cause this error. One of the most common reasons is the use of the sort function on a DataFrame object instead of a Series object. DataFrame objects do not have a sort function, and the function should be used on a Series object instead. Another reason is that the sort() method is not available in the DataFrame object. Instead, the DataFrame object has a sort_values() method that is used to sort the rows of a DataFrame based on one or more columns. This method is available in the pandas library version 0.17.0 or later.

Caution Make sure that you are using the correct method for sorting a DataFrame, because sort() method was deprecated from pandas for DataFrames.

The Solution #

To solve this error, you can use the sort_values() function on a DataFrame object to sort the data. This function sorts the DataFrame by one or more columns and returns a new DataFrame object.

For example, to solve the previous error, and to sort the DataFrame based on the 'Age' column, you can use the following code snippet:


import pandas as pd
data = {'name': ['John', 'Jane', 'Bob', 'Alice'],
        'age': [25, 30, 20, 40]
       }
df = pd.DataFrame(data)
df.sort_values(by='age')

This will return the following DataFrame, sorted by the 'age' column:



   name  age
2   Bob   20
0  John   25
1  Jane   30
3  Alice  40

Alternatively, you can also use the sort_index() method to sort a DataFrame by its index. This can be useful if you want to sort the DataFrame based on the order of the rows.

To sort the DataFrame in descending order, you can use the following code snippet:


import pandas as pd
data = {'name': ['John', 'Jane', 'Bob', 'Alice'],
        'age': [25, 30, 20, 40]
       }
df = pd.DataFrame(data)
df.sort_values(by='age', ascending=False)
Tip Remember the sort_values() method is not inplace, therefore if you want to modify the dataframe in place you should add the inplace=True parameter in the function.

The Conclusion

In conclusion, the AttributeError: 'DataFrame' object has no attribute 'sort' error occurs when trying to use the sort function on a DataFrame object in Python's pandas library. This error is caused by the use of the sort function on a DataFrame object instead of a Series object. To solve this error, you can use the sort_values() function on a DataFrame object.

For further Reference, you can check the official pandas documentation for Dataframe sorting

🔥 Join developers growing publicly
Share your knowledge, build in public, and grow your developer presence with a global community.

More Posts

Resolved: Attributeerror: 'dataframe' object has no attribute 'reshape'

Honey - Jun 20, 2024

TypeScript Complexity Has Finally Reached the Point of Total Absurdity

Karol Modelski - Apr 23

Dashboard Operasional Armada Rental Mobil dengan Python + FastAPI

Masbadar - Mar 12

Understanding and debugging Attributeerror: 'str' object has no attribute 'sort'

Russell Trey - Nov 23, 2025

Attributeerror: 'dict' object has no attribute 'iteritems'

Henry Paul - Jan 14
chevron_left
123 Points2 Badges
1Posts
0Comments
I’m a developer who enjoys building things, solving problems, and learning new technologies along th... Show more

Related Jobs

View all jobs →

Commenters (This Week)

2 comments
1 comment
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!