Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas index isin method

I'm having throuble working with the isin method when working with pandas indexes, it always returns False.

from pandas import DataFrame
df = DataFrame(data=[['a', 1], ['b', 2], ['c', 3]], index=['N01', 'N02', 'N03'])
df.index.isin(['01', '02'])

returns

array([False, False, False], dtype=bool)
like image 339
George Avatar asked Mar 08 '23 22:03

George


1 Answers

Use str.contains and pass a regex pattern:

In[5]: df.index.str.contains('01|02')

Out[5]: array([ True,  True, False], dtype=bool)

isin looks for exact matches which is why you get all False array returned

like image 69
EdChum Avatar answered Mar 17 '23 03:03

EdChum