5

I am trying to implement the code

sort_order = {
'Documentary':0,
'Film-Noir':1,
'Biography':2,
'History':3,
'War':4,
'News':5,
'Animation':6,
'Musical':7,
'Music':8,
'Drama':9
}

df.sort_values(by=['genre'], key=lambda x: x.map(sort_order))

But I receive this error:

TypeError: sort_values() got an unexpected keyword argument 'key'

I downloaded the latest version of Pandas to try and fix this using conda update pandas but this did not change anything. I know something is going wrong because their page here shows that key should be recognized.

Any help would be greatly appreciated.

6
  • your version of Pandas might not be recent. I'd suggest you update to the latest version Commented Nov 1, 2020 at 6:01
  • I have already tried that. I used this 'conda update pandas' to update it. For some reason, it still doesn't work though. Commented Nov 1, 2020 at 6:03
  • what is your pandas version number? Commented Nov 1, 2020 at 6:03
  • I think last time I checked it was 1.0.5 Commented Nov 1, 2020 at 6:05
  • 4
    You need to upgrade to 1.1 Commented Nov 1, 2020 at 6:05

1 Answer 1

6

As mentioned in the comments section, Sorting with Keys is introduced in version 1.1.0.

You may run the following to update the package (note the -U flag):

python -m pip install -U pandas
Sign up to request clarification or add additional context in comments.

1 Comment

This solution may be inspiring for people who don’t have access to this version.

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.