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!