sebaesp
sebaesp

Reputation: 31

Filtering rows containing a string pattern from a Pandas dataframe index

I need to filter rows containing a string pattern from a Pandas dataframe index.

I found the following example: How to filter rows containing a string pattern from a Pandas dataframe where dataframe is filtered with df[df["col"].str.contains()] which works fine with the example.

df = pd.DataFrame({'vals': [1, 2, 3, 4], 'ids': [u'aball', u'bball', u'cnut', u'fball']})

In the example, if I copy the column "ids" to the index, I may use df.index.str.contains("ball"), which also works fine.

However, when I use df.index.str.contains("Example") in my dataframe it does not work.

I think it doesn't work because in my dataframe the value returned is not an array([ True, False ... , True], dtype=bool), but an Index([True, False ... , True], dtype='object', length = 667).

How can I reformulate my code so it works?

I don't paste my dataframe, because im reading it from a big excel sheet.

Thank you!

Upvotes: 3

Views: 9693

Answers (1)

Romain
Romain

Reputation: 21888

You should ensure that your index is a string. The example below produces an error.

# Test data
df = DataFrame([1,2,3,4], index=['foo', 'foo1', 'foo2', 1], columns=['value'])
df[df.index.str.contains('foo')]

Converting the index to str permits to obtain the expected result.

df.index = df.index.astype('str')
df[df.index.str.contains('foo')]

      value
foo       1
foo1      2
foo2      3

Upvotes: 9

Related Questions