Pandas: Choosing an Array of Index Tags with .loc

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!

+6
source share
1 answer

This is a bug in 0.12. Version 0.13 fixes this (IOW, shortcut selection, whether the number or line should work when transferring the list).

You could do this (using the internal method):

 In [10]: df.iloc[df.index.get_indexer([2,7])] Out[10]: AB 2 2.2 7.2 7 1.4 6.3 
+7
source

Source: https://habr.com/ru/post/957621/


All Articles