I have a dataframe with lots of categories. Here list of some of them
JavaScript
x
8
1
Bank
2
3
(0827) ОСП
4
(0283) Банк ВТБ (ПАО)
5
(0822) ОСИП_ПЕНСЫ
6
(0260) АО Тинькофф Банк
7
(0755) ПАО Совкомбанк
8
I want to filter dataframe based on string matching. I don’t want to pass entire row name, i wanna pass something like [‘Совкомбанк’, ‘Тинькофф’]. The expecting result of this is :
JavaScript
1
3
1
(0260) АО Тинькофф Банк
2
(0755) ПАО Совкомбанк
3
I tried df = df[df[column_name].isin(values)]
but i didn’t work.
Advertisement
Answer
.isin
will check for exact match. What you are looking for is .str.contains
:
JavaScript
1
3
1
match_strs = ['Совкомбанк', 'Тинькофф']
2
df = df[df[column_name].str.contains("(" + "|".join(match_strs) + ")")]
3
You can have custom regular expressions within str.contains(...)
to search for whatever you want.