TypeError: pivot_table() got an unexpected keyword argument 'rows'

时间:2022-09-19 04:00:17

利用Python进行数据分析》第二章,处理MovieLens 1M数据集,有句代码总是报错:

mean_rating = data.pivot_table('rating', rows='title', cols='gender', aggfunc='mean')

报错信息如下:

Traceback (most recent call last):
  File "D:\Users\wangshuang829\AppData\Local\Continuum\Anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 3035, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-47-d6e7435a4a95>", line 1, in <module>
    mean_rating = data.pivot_table('rating', rows='title', cols='gender', aggfunc='mean')
TypeError: pivot_table() got an unexpected keyword argument 'rows'

修改办法:rows改成index,cols写成全名”columns”

mean_rating = data.pivot_table('rating', index='title', columns='gender', aggfunc='mean')

修改后得到正确处理结果:

gender                                F         M
title
$1,000,000 Duck (1971)         3.375000  2.761905
'Night Mother (1986)           3.388889  3.352941
'Til There Was You (1997)      2.675676  2.733333
'burbs, The (1989)             2.793478  2.962085
...And Justice for All (1979)  3.828571  3.689024