Note: since I asked this question, .ix
still exists but more or less has been replaced by .loc
for label-based indexing and .iloc
for positional indexing.
Having read the docs one the ix
method of DataFrames, I'm a bit confused by the following behavior with my MultiIndexed DataFrame (specifying select columns of the index).
In [57]: metals
Out[57]:
<class 'pandas.core.frame.DataFrame'>
MultiIndex: 24245 entries, (u'BI', u'Arsenic, Dissolved', -2083768576.0, 1.0)
to (u'WC', u'Zinc, Total', 1661183104.0, 114.0)
Data columns:
Inflow_val 20648 non-null values
Outflow_val 20590 non-null values
Inflow_qual 20648 non-null values
Outflow_qual 20590 non-null values
dtypes: float64(2), object(2)
In [58]: metals.ix['BI'].shape # first column in the index, ok
Out[58]: (3368, 4)
In [59]: metals.ix['BI', :, :, :].shape # first + other columns, ok
Out[59]: (3368, 4)
In [60]: metals.ix['BI', 'Arsenic, Dissolved'].shape # first two cols
Out[60]: (225, 4)
In [61]: metals.ix['BI', 'Arsenic, Dissolved', :, :].shape # first two + all others
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-62-1fb577ec32fa> in <module>()
----> 1 metals.ix['BI', 'Arsenic, Dissolved', :, :].shape
# traceback spaghetti snipped
KeyError: 'no item named Arsenic, Dissolved'
In [62]: metals.ix['BI', 'Arsenic, Dissolved', :, 1.0].shape # also fails
It took me a long time to realize that what I had been trying to achieve with In [61]
was possible with In [60]
. Why does the ix
method behave like this? What I'm really trying to get at is the scenario at In [62]
.
My guess is that I need to redefine the index hierarchy, but I'm curious if there's an easier way.
Thanks.