I have dataframe:
Col1 Col2
Rowab1 3 5
Rowbc2 4 6
Rowxy3 7 2
I want to search the dataframe and only keep the row where the row name contains "bc" like this while deleting rest of the rows:
Col1 Col2
Rowab1 3 5
Rowxy3 7 2
I have the following piece of code:
df.loc[df.index.isin(['bc'])]
However, this does not search for "bc" within a string but it searches for an entire independent string "bc". Is there any "like" operator that I can possibly incorporate such as
df.loc[df.index.isin(['%bc%'])] ?
Thank you!
An alternative with pd.Series.str.find
:
In [41]: df
Out[41]:
Col1 Col2
Rowab1 3 5
Rowbc2 4 6
Rowxy3 7 2
In [42]: df[df.index.str.find('bc') > -1]
Out[42]:
Col1 Col2
Rowbc2 4 6