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

原文地址:https://www.cnblogs.com/S-yesir/p/4807516.html