Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering rows containing a string pattern from a Pandas dataframe index

Tags:

python

pandas

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!

like image 651
sebaesp Avatar asked Aug 04 '26 17:08

sebaesp


1 Answers

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
like image 134
Romain Avatar answered Aug 07 '26 17:08

Romain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!