SteelyDanish SteelyDanish - 3 months ago 12
Python Question

How to select by categorical index of grouped data using Pandas

I am trying to select the data from a grouped categorical index and am unable to select by group.

This data was amassed using pd.cut in bins of 60. The final goal is to select a range of the index then plot the data - however I can't select the data.

For example - I want to select a slice of (0,60] and (60,120] but am unable to slice that. Any help much appreciated.

Curret dataframe data:

enter image description here

Thanks

Answer

pd.cut just creates string values, so you can give them in a list directly using .ix or just do location indexing with .iloc:

In [7]: df
Out[7]:
            0    1
1
(-1, 0]  0.00  0.0
(0, 1]   0.01  1.0
(1, 2]   0.02  2.0
(2, 4]   0.03  3.0

In [8]: df.ix[['(0, 1]', '(1, 2]']]
Out[8]:
           0    1
1
(0, 1]  0.01  1.0
(1, 2]  0.02  2.0

In [9]: df.iloc[1:3]
Out[9]:
           0    1
1
(0, 1]  0.01  1.0
(1, 2]  0.02  2.0
Comments