Python Pandas Accessing values from second index in multi-indexed dataframe
You can use xs
:
In [11]: df.xs(2, level='B')
Out[11]:
Value
A
1 6.87
2 9.87
alternatively:
In [12]: df1.xs(1, level=1)
Out[12]:
Value
A
1 5.67
2 8.67
Just as an alternative, you could use df.loc
:
>>> df.loc[(slice(None),2),:]
Value
A B
1 2 6.87
2 2 9.87
The tuple accesses the indexes in order. So, slice(None)
grabs all values from index 'A'
, the second position limits based on the second level index, where 'B'=2
in this example. The :
specifies that you want all columns, but you could subet the columns there as well.