<Python, pandas> pivot_tableの使い方とプロット。

過去メモもあるが改めてメモメモ。

In [74]: import pandas as pd

In [75]: import numpy as np

In [76]: df = pd.DataFrame({'A' : ['one', 'one', 'two', 'three'] * 6,
    ...:                    'B' : ['A', 'B', 'C'] * 8,
    ...:                    'C' : ['foo', 'foo', 'foo', 'bar', 'bar', 'bar'] * 4,
    ...:                    'D' : np.random.randn(24),
    ...:                    'E' : np.random.randn(24)})

In [77]: pv = pd.pivot_table(df, values='D', rows=['B'], cols=['A', 'C'], aggfunc=np.sum)

In [78]: pv.plot(kind='bar')

Out[78]: <matplotlib.axes._subplots.AxesSubplot at 0x9905828>

f:id:nekoyukimmm:20150410093151p:plain

お勉強した場所は、 Reshaping and Pivot Tablesの、

Reshaping and Pivot Tables — pandas 0.13.1 documentation

と、API Reference

pandas.tools.pivot.pivot_table — pandas 0.16.0 documentation

と、とっても詳しいここ。

sinhrks.hatenablog.com