In [1]: arrays = [[’bar’, ’bar’, ’baz’, ’baz’, ’foo’, ’foo’, ’qux’, ’qux’],
...: [’one’, ’two’, ’one’, ’two’, ’one’, ’two’, ’one’, ’two’]]
In [2]: tuples = list(zip(*arrays))
In [4]: index = MultiIndex.from_tuples(tuples, names=[’first’, ’second’])
In [6]: s = Series(randn(8), index=index)
In [7]: s
Out[7]:
firstsecond
bar one0.469112two -0.282863
baz one -1.509059two -1.135632
foo one1.212112two -0.173215
qux one0.119209two -1.044236
dtype: float64
In [16]: df = DataFrame(randn(3, 8), index=[’A’, ’B’, ’C’], columns=index)
In [17]: df
Out[17]:
first bar baz foo qux \
secondonetwoonetwoonetwoone
A 0.8957170.805244 -1.2064122.5656461.4312561.340309 -1.170299
B 0.4108350.8138500.132003 -0.827317 -0.076467 -1.1876781.130127
C -1.4136811.6079201.0241800.5696050.875906 -2.2113720.974466firstsecondtwo
A -0.226169
B -1.436737
C -2.006747
In [41]: def mklbl(prefix,n):
....: return ["%s%s" % (prefix,i) for i in range(n)]
In [42]: miindex = MultiIndex.from_product([mklbl(’A’,4),
....: mklbl(’B’,2),
....: mklbl(’C’,4),
....: mklbl(’D’,2)])
In [43]: micolumns = MultiIndex.from_tuples([(’a’,’foo’),(’a’,’bar’),
....: (’b’,’foo’),(’b’,’bah’)],
....: names=[’lvl0’, ’lvl1’])
In [44]: dfmi = DataFrame(np.arange(len(miindex)*len(micolumns)).reshape((len(miindex),len(micolumns))),index=miindex,columns=micolumns).sortlevel().sortlevel(axis=1)
In [45]: dfmi
Out[45]:
lvl0 a b
lvl1 bar foo bah foo
A0 B0 C0 D0 1032
D1 5476
C1 D0 981110
D1 13121514
C2 D0 17161918
D1 21202322
C3 D0 25242726...............
A3 B1 C0 D1 229228231230
C1 D0 233232235234
D1 237236239238
C2 D0 241240243242
D1 245244247246
C3 D0 249248251250
D1 253252255254
In [47]: idx = pd.IndexSlice
In [51]: mask = dfmi[(’a’,’foo’)]>200
In [52]: dfmi.loc[idx[mask,:[’C1’,’C3’]],idx[:,’foo’]]
Out[52]:
lvl0 a b
lvl1 foo foo
A3 B0 C1 D1 204206
C3 D0 216218
D1 220222
B1 C1 D0 232234
D1 236238
C3 D0 248250
D1 252254