Consider this data file:
df = pd.DataFrame({u'A': {2.0: 2.2, 7.0: 1.4, 8.0: 1.4, 9.0: 2.2}, u'B': {2.0: 7.2, 7.0: 6.3, 8.0: 4.4, 9.0: 5.0}})
Which looks like this:
AB 2 2.2 7.2 7 1.4 6.3 8 1.4 4.4 9 2.2 5.0
I would like to get indexes labeled 2 and 7 (numbers, not strings)
df.loc[[2, 7]]
gives an error!
IndexError: indices are out-of-bounds
However, df.loc[7] and df.loc[2] work fine and as expected. Also, if I define a dataframe index with strings instead of numbers:
df2 = pd.DataFrame({u'A': {'2': 2.2, '7': 1.4, '8': 1.4, '9': 2.2}, u'B': {'2': 7.2, '7': 6.3, '8': 4.4, '9': 5.0}}) df2.loc[['2', '8']]
It works great.
This is not the behavior that I expected from df.loc (is it an error or just gotcha?) Can I pass an array of numbers as indices of labels, and not just positions?
I can convert all indexes to strings and then work with .loc , but that would be very inconvenient for the rest of my code.
Thank you for your time!
source share