Sometimes while dealing with data frames, we might face AttributeError: ‘dataframe’ object has no attribute ‘sort’ error. This error usually occurs when we try to sort the numeric values in the data frame using the sort() method. Although, the sort function in Python is used to sort the iterable objects like lists directly applying it to data frame will cause an error. In this short article, we will learn how we can fix the AttributeError: dataframe’ object has no attribute ‘sort’ error using methods and will cover various ways to sort the data frame in Python without getting any error:
AttributeError: ‘dataframe’ object has no attribute ‘sort’ – Possible Solutions
We know that this error occurs when we try to sort the data frame using the sort() method in Python. However, we cannot directly apply the sort() method on the data frame to sort the elements. We need to apply to sort in different ways.
Let us first take an example and see how and when we are getting AttributeError: dataframe’ object has no attribute ‘sort’ error. Let us assume that we have the following data frame.
# importing the modules
import pandas as pd
import numpy as np
# radom
np.random.seed(0)
# creating a data fram
df = pd.DataFrame({'col1': list('pqrstuv'), 'col2': np.random.choice(10, 7)})
# dataframe
df
Output:

Now, let us say that we want to sort the data in ascending order and we applied sort() method as shown below:
# sorting the dataframe
df.sort()
Output:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/tmp/ipykernel_476339/2522624421.py in <module>
1 # sorting the dataframe
----> 2 df.sort()
~/.local/lib/python3.10/site-packages/pandas/core/generic.py in __getattr__(self, name)
5905 ):
5906 return self[name]
-> 5907 return object.__getattribute__(self, name)
5908
5909 def __setattr__(self, name: str, value) -> None:
AttributeError: 'DataFrame' object has no attribute 'sort'
The reason for getting this error because the data frame object does not have attribute sort(). Now let us go through various methods to solve the problem and sort the data frame.
Solution: Using sort_values() method
We know that the data frame object does not have the method named sort() instead we can use the sort_values() method which takes the name of the column as a parameter value and sorts the data frame based on the given column name.
Let us use the sort_values() method to sort the data frame based on column two.
# sorting the dataframe
df = df.sort_values(["col2"])
# dataframe
df
Output:

Notice that we sorted the data frame based on a single column using the sort_values() method.
The sort_values() method is very powerful and it can be used to sort the data frame based on multiple-column values as well. For example, now we will sort the data frame based on col1 and col2 respectively.
# sorting the dataframe
df = df.sort_values(['col1', "col2"])
# dataframe
df
Output:

As you can see, we sorted the data frame based on multiple columns:
We can use the sort_values() method to sort the values in descending order as well. We just need to define ascending as False in order to sort the values in descending order as shown below:
# sorting the dataframe
df = df.sort_values(["col2"], ascending=False)
# dataframe
df
Output:

Notice that the data frame is now in descending order based on column two.
Hopefully, using any of the given methods will help you to get rid of AttributeError: ‘DataFrame’ object has no attribute ‘sort’ error.
Summary
In this short article, we discussed how we can solve the AttributeError: ‘DataFrame’ object has no attribute ‘sort’ error using different methods. We learned how we can sort the data frame based on single and multiple columns.
Related Issues
- AttributeError: module ‘seaborn’ has no attribute ‘histplot’
- TypeError: only size-1 arrays can be converted to Python scalars solved
- Importerror: numba needs numpy 1.21 or less-Solved
- Typeerror: type numpy.ndarray doesnt define round method solved
- AttributeError: ‘list’ object has no attribute ‘split’
- ModuleNotFoundError: No module named ‘cv2’
- TypeError: not all arguments converted during string formatting
- Typeerror: ‘float’ object is not iterable