‘dataframe’ object has no attribute ‘sort’ – Python Error

Total
0
Shares

Python throws the error, ‘dataframe’ object has no attribute ‘sort’, because Pandas deprecated sort() function in favor of sort_values() and sort_index().

As, the name indicates, sort_values() is used to sort a dataframe by value and sort_index() sorts it by index.

Consider this example –

np.random.seed(0)
df = pd.DataFrame({'A': list('accab'), 'B': np.random.choice(10, 5)})
df
   A  B
0  a  7
1  c  9
2  c  3
3  a  5
4  b  2

In this code, we are defining a DataFrame, df, with two columns, A and B. The first column is the array of characters, ['a', 'c', 'c', 'a', 'b']. The second column is the random number array with 5 values and maximum weight of 10.

Now, in order to sort this dataframe, we can use sort_values() or sort_index().

To sort it by value and using single column –

df.sort_values(by='A')

   A  B
0  a  7
3  a  5
4  b  2
1  c  9
2  c  3

To sort it by multiple columns –

df.sort_values(by=['A', 'B'])

   A  B
3  a  5
0  a  7
4  b  2
2  c  3
1  c  9

To sort it by index, use sort_index() –

df.sort_index()

   A  B
0  a  7
1  c  9
2  c  3
3  a  5
4  b  2

    Tweet this to help others

Sign Up for Our Newsletters

Get only the best articles delivered to your mail

You May Also Like
python run bash script in background

python run bash script in background

Table of Contents Hide IntroductionTerminating background script after a timeoutHow to check if process is still running or terminated?How to get output from process?ConclusionLive DemoRelated Posts: Python subprocess library is…
View Post