How to Solve Python AttributeError: ‘DataFrame’ object has no attribute ‘sort’

by | Programming, Python, Tips

A DataFrame is a two-dimensional, mutable tabular data structure like an Excel spreadsheet. You can sort the columns of a DataFrame. As of version 0.20, the sort method is deprecated. If you want to sort a DataFrame, you can use DataFrame.sort_values and DataFrame.sort_index. If you try to call sort on a DataFrame, you will raise the AttributeError: ‘DataFrame’ object has no attribute ‘sort’.

This tutorial will go through how to solve this error with the help of code examples.


AttributeError: ‘DataFrame’ object has no attribute ‘sort’

AttributeError occurs in a Python program when we try to access an attribute (method or property) that does not exist for a particular object. The part “‘DataFrame’ object has no attribute ‘sort’” tells us that the DataFrame object we are handling does not have the sort attribute. The sort() method is deprecated as of version 0.20 and is replaced by DataFrame.sort_values() and DataFrame.sort_index().

The DataFrame.sort_index method sorts the DataFrame object by labels along an axis, either by rows or columns. The DataFrame.sort_values method sorts the DataFrame object by the values along either the column or index axis.

Example

Let’s look at an example where we want to sort a DataFrame where one column contains indices and the second column contains random integer values between 0 and 1000. Let’s look at the data:

import numpy as np

import pandas as pd

np.random.seed(0)

df = pd.DataFrame({'Col1':list('aacddfg'), 'Col2':np.random.choice(1000, 7)})

print(df)
  Col1  Col2
0    a   684
1    a   559
2    c   629
3    d   192
4    d   835
5    f   763
6    g   707

Let’s try to sort the DataFrame by the Col2 column:

sorted_df = df.sort("Col2")

Let’s run the code to see the result:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-28-4869c4757ae2> in <module>
----> 1 sorted_df = df.sort("Col2")

~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/generic.py in __getattr__(self, name)
   5581         ):
   5582             return self[name]
-> 5583         return object.__getattribute__(self, name)
   5584 
   5585     def __setattr__(self, name: str, value) -> None:

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

The error occurs because the sort() method is deprecated from Pandas version 0.20.

Solution

Sort by Single Column

We can sort the DataFrame through two methods. First, we will look at sorting using sort_values(). We can pass the column name to the by parameter to sort by the single column. Let’s look at the revised code:

sorted_df = df.sort_values(by='Col2')

print(sorted_df)

Let’s run the code to see the result:

 Col1  Col2
3    d   192
1    a   559
2    c   629
0    a   684
6    g   707
5    f   763
4    d   835

Sort by Multiple Columns

We can sort by both columns Col1 and Col2 in df by passing a list to sort_values. Let’s look at the revised code:

sorted_df = df.sort_values(by=['Col1','Col2'])

print(sorted_df)

Let’s run the code to see the result:

  Col1  Col2
1    a   559
0    a   684
2    c   629
3    d   192
4    d   835
5    f   763
6    g   707

Sort by DataFrame Index

We can sort by the index of a DataFrame using sort_index. The indices of a DataFrame are the numerical values assigned to each row of the DataFrame. Let’s look at the revised code:

print(sorted_df,'\n')

sorted_by_index_df = sorted_df.sort_index()

print(sorted_by_index_df)

Let’s run the code to get the result:

  Col1  Col2
1    a   559
0    a   684
2    c   629
3    d   192
4    d   835
5    f   763
6    g   707 

  Col1  Col2
0    a   684
1    a   559
2    c   629
3    d   192
4    d   835
5    f   763
6    g   707

We can see that the first DataFrame is sorted by Col2 and the second DataFrame is sorted by index.

Summary

Congratulations on reading to the end of this tutorial! The AttributeError ‘DataFrame’ object has no attribute ‘sort’ occurs when you try to call the datetime sort() method on a DataFrame object.

To solve this error, you can use the Pandas methods DataFrame.sort_values() and DataFrame.sort_index().

For further reading on Series, go to the articles:

To learn more about Python for data science and machine learning, go to the online courses page on Python for the most comprehensive courses available.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee