How to work with data indexed by floats in pandas

Pandas has no issue if the index level is a single level so not a multi index:

In [178]:

frame = frame.set_index(['a'])
frame.loc[1.2]
Out[178]:
      b     v
a            
1.2  30   123
1.2  60  1234

If you do have a multi-index then you can get generate a mask using the index level 0 (the first) and use this to select the values:

In [180]:

mask = frame.index.get_level_values(0)
frame.loc[mask == 1.2]
Out[180]:
           v
a   b       
1.2 30   123
    60  1234

The mask itself contains all the level 0 values for each row:

In [181]:

mask
Out[181]:
Float64Index([1.2, 1.2, 3.0, 3.0], dtype='float64')

It is better and more explicit to specify the level using the name:

mask = frame.index.get_level_values('a')

Tags:

Python

Pandas