在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