1. 程式人生 > >TypeError: pivot_table() got an unexpected keyword argument 'rows'

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

利用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