How to work with data indexed by floats in pandas
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Light Drops
--
Chapters
00:00 Question
01:06 Accepted answer (Score 4)
01:59 Answer 2 (Score 1)
02:43 Thank you
--
Full question
https://stackoverflow.com/questions/2457...
Question links:
[string index]: http://pandas.pydata.org/pandas-docs/sta...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #pandas
#avk47
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Light Drops
--
Chapters
00:00 Question
01:06 Accepted answer (Score 4)
01:59 Answer 2 (Score 1)
02:43 Thank you
--
Full question
https://stackoverflow.com/questions/2457...
Question links:
[string index]: http://pandas.pydata.org/pandas-docs/sta...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #pandas
#avk47
ACCEPTED ANSWER
Score 4
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')
ANSWER 2
Score 1
Came across this while trying something similar and it worked without issue. Either the pandas library has improved, or you are missing inplace (or assignment) in set_index.
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)
f2 = frame.set_index(['a', 'b']) # <<<<<<<<<
print(f2)
v
a b
1.2 30 123
60 1234
3.0 30 12345
60 123456
Now f2.loc[1.2] works.
print(f2.loc[1.2])
v
b
30 123
60 1234