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