TypeError:pivot_table()得到一个意外的关键字参数'rows'

Chr*_*now 22 python pandas

我正在尝试使用pandas DataFrame的pivot_table方法;

mean_ratings = data.pivot_table('rating', rows='title', cols='gender', aggfunc='mean')
Run Code Online (Sandbox Code Playgroud)

但是,我收到以下错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-55-cb4d494f2f39> in <module>()
----> 1 mean_ratings = data.pivot_table('rating', rows='title', cols='gender', aggfunc='mean')

TypeError: pivot_table() got an unexpected keyword argument 'rows'
Run Code Online (Sandbox Code Playgroud)

以上命令取自Wes McKinney(熊猫的创造者)的" Python for Data Analysis " 一书

Chr*_*now 36

我的解决方案是更改'rows => index'和'cols => columns'):

从:

mean_ratings = data.pivot_table('rating', rows='title', cols='gender', aggfunc='mean')
Run Code Online (Sandbox Code Playgroud)

至:

mean_ratings = data.pivot_table('rating', index='title', columns='gender', aggfunc='mean')
Run Code Online (Sandbox Code Playgroud)

  • 不,这不是一个错误,这些只是旧的关键字已被弃用了一段时间,现在已被删除.因此你得到了错误 (3认同)
  • 喔好吧.希望这个stackoverflow帖子能够帮助其他在阅读本书时遇到API变化的用户. (2认同)