How to select the inner matrix of a multiple index series in python?

ipython

#1

I have created a series in which there is level corresponding to each subset of a series.

data = Series(np.random.randn(10),
.....: index=[['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'd', 'd'],
.....: [1, 2, 3, 1, 2, 3, 1, 2, 2, 3]])

data
a 1 0.670216
2 0.852965
3 -0.955869
b 1 -0.023493
2 -2.304234
3 -0.652469
c 1 -1.218302
2 -1.332610
d 2 1.074623
3 0.723642

I am able to select the data of a series through the outer index(level) of a series.I want to know how to select the data of a series using inner index.

data['b']
1 -0.023493
2 -2.304234
3 -0.652469

#2

@sid100158,

I would suggest to use xs method of DataFrame. It takes a additional level argument to make selecting data at a particular level of a MultiIndex easier. Let’s look at the below code:

data = pd.Series(np.random.randn(10),index=[['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'd', 'd'],[1, 2, 3, 1, 2, 3, 1, 2, 2, 3]])

Now to select data based on inner index:

data.xs(1, level=1)

Regards,
Mark