How to work with data indexed by floats in pandas
pandas, python
Solution
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')
Problem
I use pandas `DataFrame` with hierarhical index, and in one particular case it is indexed by float values. Here is example: ``` example_data = [ {'a': 1.2, 'b':30, 'v':123}, {'a': 1.2, 'b':60, 'v':1234}, {'a': 3, 'b':30, 'v':12345}, {'a': 3, 'b':60, 'v':123456}, ] frame = pd.DataFrame(example_data) frame.set_index(['a', 'b']) ``` Now I'd like to use partial indexing to select frame with `a==1.2` and then display it. Documentation shows how to do this for string index, but this approach obviously doesn't work for floats, irrevelant whether I try `frame.loc[1.2]` i get error about 1.2 being imporper for `Int64Index` which is obviously true since i use float for indexing. Is there any way to work with float index in pandas? How can I fix my Hierarhical Index? Actual error message was: ``` TypeError: the label [1.2] is not a proper indexer for this index type (Int64Index) ```