
174
3
章
pandas
を使ったデータ操作
In[8]: titanic.pivot_table(index='sex', columns='class',
aggfunc={'survived':sum, 'fare':'mean'})
Out[8]: fare survived
class First Second Third First Second Third
sex
female 106.125798 21.970121 16.118810 91.0 70.0 72.0
male 67.226127 19.741782 12.661633 45.0 17.0 47.0
この例では
values
キーワードが省略されていることにも注意してください。
aggfunc
にマッピン
グを指定するとき、値は自動的に決定されます。時には、各グループに沿った合計を加えると便利
な場合があります。これは、
margins
キーワードで指定できます。
In[9]: titanic.pivot_table('survived', index='sex', columns='class', margins=True)
Out[9]: class First Second Third All
sex
female 0.968085 0.921053 0.500000 0.742038
male 0.368852 0.157407 0.
135447 0.188908
All 0.629630 0.472826 0.242363 ...