3

At work we recently upgraded to pandas 0.20 and I have a list of numbers that I sort using sort (however this is no longer supported and I am getting the above message when I try sort_values).

numbers = [1, 3, 4, 2] 
numbers.sort(reverse = True) 
print numbers

[4, 3, 2, 1]

numbers.sort_values(reverse = True)

I'm getting this error :

Traceback (most recent call last):

File "", line 1, in

AttributeError: 'list' object has no attribute 'sort_values'

2
  • 1
    try : pd.Series(numbers).sort_values() Commented Jul 26, 2019 at 12:34
  • Possible duplicate of Sort a list in python Commented Jul 26, 2019 at 12:35

3 Answers 3

5

You don't appear to be using pandas at all here; numbers is a standard Python list. And the method to sort a list is just called sort.

numbers.sort(reverse=True)
Sign up to request clarification or add additional context in comments.

1 Comment

cheers, I'm still a novice at this and I used this example without understanding numbers was a standard python list. so I guess if I called the list of number "list" and reran it then I get the same error >>> lst = [1, 3, 4, 2] >>> lst.sort_values(reverse = True) Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'list' object has no attribute 'sort_values' >>>
4

Use sorted():

lst = [1, 2, 3, 4]
new_lst = sorted(lst, reverse=True)

1 Comment

thanks, that however only prints out the lst in the order required and doesn't store it as required
1

I ended up using sort() rather than sort(reverse = True) and it worked as I wanted, thanks for the help, I really appreciate it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.