《利用python進行數據挖掘》pivot_table()方法報錯


在MovieLens 1M數據集其中一個例子,使用pivot_table()按性別計算每部電影的平均得分

1 mean_ratings = data.pivot_table('rating', rows='title', cols='gender', aggfunc='mean')
2 print mean_ratings[:5]

報錯信息:

Traceback (most recent call last):
File "/Users/huanghonglin/PycharmProjects/DataMining/demo2.py", line 26, in <module>
mean_ratings = data.pivot_table('rating', rows='title', cols='gender', aggfunc='mean')
TypeError: pivot_table() got an unexpected keyword argument 'rows'

解決問題:

將 rows 替換成 index;

將 cols 替換成 columns。

1 mean_ratings = data.pivot_table('rating', index='title', columns='gender', aggfunc='mean')
2 print mean_ratings[:5]

輸出結果

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM