3

Suppose I have a pandas dataframe like this:

         Word      Ratings
   0     TLYSFFPK  1
   1     SVLENFVGR 2
   2     SVFNHAIRK 3
   3     KAGEVFIHK 4

How can I use regex in pandas to filter out the rows that have the word that match the following regex pattern but keep the dataframe formatting? The regex pattern is: \b.[VIFY][MLFYIA]\w+[LIYVF].[KR]\b

Expected output:

         Word    Ratings
   1     SVLENFVGR 2
   2     SVFNHAIRK 3

1 Answer 1

12

Demo:

In [2]: df
Out[2]:
        Word  Ratings
0   TLYSFFPK        1
1  SVLENFVGR        2
2  SVFNHAIRH        3
3  KAGEVFIHK        4

In [3]: pat = r'\b.[VIFY][MLFYIA]\w+[LIYVF].[KR]\b'

In [4]: df.Word.str.contains(pat)
Out[4]:
0    False
1     True
2    False
3    False
Name: Word, dtype: bool

In [5]: df[df.Word.str.contains(pat)]
Out[5]:
        Word  Ratings
1  SVLENFVGR        2
Sign up to request clarification or add additional context in comments.

1 Comment

You are always a time saver for me :)

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.