为什么“key”是 sort_values() 的意外关键字

Bio*_*277 5 python sorting typeerror dataframe pandas

我正在尝试实现代码

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))
Run Code Online (Sandbox Code Playgroud)

但我收到此错误:

TypeError: sort_values() got an unexpected keyword argument 'key'
Run Code Online (Sandbox Code Playgroud)

我下载了最新版本的 Pandas 来尝试使用 conda update pandas 修复此问题,但这并没有改变任何内容。我知道出了问题,因为他们的页面显示应该识别密钥。

任何帮助将不胜感激。

Tib*_*. M 6

正如评论部分提到的,使用键排序是在 1.1.0 版本中引入的

您可以运行以下命令来更新软件包(请注意该-U标志):

python -m pip install -U pandas
Run Code Online (Sandbox Code Playgroud)