In [1]:
import numpy as np
import pandas as pd
from pandas import Series, DataFrame

In [2]:
animals = DataFrame(np.arange(16).reshape(4,4), columns = list('WXYZ'), index=['Dog', 'Cat', 'Bird', 'Mouse'])

In [3]:
animals


Out[3]:
W X Y Z
Dog 0 1 2 3
Cat 4 5 6 7
Bird 8 9 10 11
Mouse 12 13 14 15

In [4]:
animals.ix[1:2, ['W','Y']] = np.nan
animals


Out[4]:
W X Y Z
Dog 0.0 1 2.0 3
Cat NaN 5 NaN 7
Bird 8.0 9 10.0 11
Mouse 12.0 13 14.0 15

In [6]:
behavior_map = {'W': 'good', 'X': 'bad', 'Y': 'good', 'Z':'bad'}

In [8]:
animal_col = animals.groupby(behavior_map, axis=1)

In [10]:
animal_col.sum()


Out[10]:
bad good
Dog 4.0 2.0
Cat 12.0 NaN
Bird 20.0 18.0
Mouse 28.0 26.0

In [11]:
behave_series = Series(behavior_map)

behave_series


Out[11]:
W    good
X     bad
Y    good
Z     bad
dtype: object

In [13]:
animals.groupby(behave_series, axis=1).count()


Out[13]:
bad good
Dog 2 2
Cat 2 0
Bird 2 2
Mouse 2 2

In [14]:
animals.groupby(len).sum()


Out[14]:
W X Y Z
3 0.0 6 2.0 10
4 8.0 9 10.0 11
5 12.0 13 14.0 15

In [15]:
keys = list('ABAB')

In [16]:
animals.groupby([len, keys]).max()


Out[16]:
W X Y Z
3 A 0.0 1 2.0 3
B NaN 5 NaN 7
4 A 8.0 9 10.0 11
5 B 12.0 13 14.0 15

In [17]:
hier_col = pd.MultiIndex.from_arrays([['NY', 'NY', 'NY', 'SF', 'SF'], [1,2,3,1,2]], names=['City', 'sub_value'])

In [18]:
dframe_hr = DataFrame(np.arange(25).reshape(5,5), columns = hier_col)

In [19]:
dframe_hr = dframe_hr * 100

In [20]:
dframe_hr


Out[20]:
City NY SF
sub_value 1 2 3 1 2
0 0 100 200 300 400
1 500 600 700 800 900
2 1000 1100 1200 1300 1400
3 1500 1600 1700 1800 1900
4 2000 2100 2200 2300 2400

In [ ]:
d d