3

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!

1
  • It may become Index([True, False ... , True], dtype='object', length = 667) if you assign df.index = df.index.str.contains('ball'). You don't need to do that. df[df.index.str.contains('ball')] works just fine. Commented Aug 14, 2016 at 14:10

1 Answer 1

9

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.