In [ ]:
'''
series:
index
unstack
dataframes:
index.names = ...
columns.names = ...
swaplevel
sortlevel
sum(level='Temp', axis=1)
'''

In [2]:
import numpy as np
import pandas as pd
from pandas import Series,DataFrame
from numpy.random import randn

In [10]:
# different levels of indexes
ser = Series(randn(6), index=[[1,1,1,2,2,2],['a','b','c','a','b','c']])

ser


Out[10]:
1  a   -1.387389
   b    0.861194
   c    0.403067
2  a   -1.745261
   b   -1.541656
   c   -1.025104
dtype: float64

In [6]:
ser.index


Out[6]:
MultiIndex(levels=[[1, 2], [u'a', u'b', u'c']],
           labels=[[0, 0, 0, 1, 1, 1], [0, 1, 2, 0, 1, 2]])

In [15]:
# select the first object from outer index
ser[1]


Out[15]:
a   -1.387389
b    0.861194
c    0.403067
dtype: float64

In [16]:
# select all rows with inner index 'a'
ser[:,'a']


Out[16]:
1   -1.387389
2   -1.745261
dtype: float64

In [18]:
# create a dataframe from multilevel index series object
# innner index become columns
# outer index become rows
df = ser.unstack()

df


Out[18]:
a b c
1 -1.387389 0.861194 0.403067
2 -1.745261 -1.541656 -1.025104

In [20]:
# pass 2 lists to index and 2 lists to column 
df2 = DataFrame(np.arange(16).reshape(4,4), index=[['a','a','b','b'],[1,2,1,2]],
               columns=[['NY','NY','LA','SF'],['cold','hot','hot','cold']])

df2


Out[20]:
NY LA SF
cold hot hot cold
a 1 0 1 2 3
2 4 5 6 7
b 1 8 9 10 11
2 12 13 14 15

In [22]:
# name index levels
df2.index.names = ['Index 1', 'Index 2']

df2.columns.names = ['Cities', 'Temp']

df2


Out[22]:
Cities NY LA SF
Temp cold hot hot cold
Index 1 Index 2
a 1 0 1 2 3
2 4 5 6 7
b 1 8 9 10 11
2 12 13 14 15

In [26]:
# interchange index levels using axis=1, by default will swap row indexes
df2.swaplevel('Cities','Temp',axis=1)


Out[26]:
Temp cold hot cold
Cities NY NY LA SF
Index 1 Index 2
a 1 0 1 2 3
2 4 5 6 7
b 1 8 9 10 11
2 12 13 14 15

In [27]:
# sort levels, 1 represents inner index and 0 represents outer index in rows
df2.sortlevel(1)


Out[27]:
Cities NY LA SF
Temp cold hot hot cold
Index 1 Index 2
a 1 0 1 2 3
b 1 8 9 10 11
a 2 4 5 6 7
b 2 12 13 14 15

In [28]:
df2


Out[28]:
Cities NY LA SF
Temp cold hot hot cold
Index 1 Index 2
a 1 0 1 2 3
2 4 5 6 7
b 1 8 9 10 11
2 12 13 14 15

In [30]:
# compute on a specific index level, for example Temp
# this will sum the 2 'hot' columns together and the 2 'cold' columns together
df2.sum(level='Temp', axis=1)


Out[30]:
Temp cold hot
Index 1 Index 2
a 1 3 3
2 11 11
b 1 19 19
2 27 27

In [ ]: