【问题标题】:Why is 'key' an unexpected keyword of sort_values()为什么'key'是sort_values()的意外关键字
【发布时间】:2021-02-14 03:20:18
【问题描述】:

我正在尝试实现代码

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))

但我收到此错误:

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

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

任何帮助将不胜感激。

【问题讨论】:

  • 您的 Pandas 版本可能不是最新版本。我建议你更新到最新版本
  • 我已经试过了。我用这个'conda update pandas'来更新它。出于某种原因,它仍然无法正常工作。
  • 你的熊猫版本号是多少?
  • 我想上次我检查它是 1.0.5
  • 你需要升级到1.1

标签: python pandas dataframe sorting typeerror


【解决方案1】:

如 cmets 部分所述,Sorting with Keys is introduced in version 1.1.0

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

python -m pip install -U pandas

【讨论】:

  • This solution 可能对无法访问此版本的人有所启发。
猜你喜欢
  • 2010-10-29
  • 1970-01-01
  • 2011-02-14
  • 2011-10-28
  • 1970-01-01
  • 2020-07-31
相关资源
最近更新 更多