A simple demonstration of pivot_table. Reshaping and Pivot Tables — pandas 0.13.1 documentation
In [1]:
import pandas as pd
from pandas import DataFrame, Series
In [2]:
df = DataFrame([{
'year':1880,
'name':'John',
'sex': 'M',
'births': 13
},
{'year':1880,
'name':'Pat',
'sex': 'M',
'births': 13
},
{'year':1880,
'name':'Pat',
'sex': 'F',
'births': 13
},
{
'year':1880,
'name':'Jane',
'sex': 'F',
'births': 20
},
{
'year':1881,
'name':'John',
'sex': 'M',
'births': 90
},
{
'year':1881,
'name':'Jane',
'sex': 'F',
'births': 21
},])
df
Out[2]:
In [3]:
pt = df.pivot_table(rows='year', cols=['name','sex'])['births']
pt
Out[3]:
In [4]:
# let's make a new table in which there is M/F subindex for all names
names = set(pt.columns.get_level_values(level=0))
sexes = set(pt.columns.get_level_values(level=1))
names, sexes
Out[4]:
In [5]:
# http://pandas.pydata.org/pandas-docs/stable/indexing.html#creating-a-multiindex-hierarchical-index-object
new_index = pd.MultiIndex.from_product([list(names), list(sexes)],
names=['name','sex'])
new_index
Out[5]:
In [6]:
# http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.reindex.html
pt.T.reindex(new_index).T
Out[6]:
In [7]:
pt.T.reindex(new_index).T.fillna(0)
Out[7]:
In [19]:
df.groupby('year').apply(lambda s: s.groupby(['name','sex']).agg('sum'))
Out[19]:
In [25]:
df.groupby('year').apply(lambda s: s.groupby(['name','sex']).agg('sum')).unstack()
Out[25]: