Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas: get the dataframe rows which value is matched with regexp

I have a dataframe as below

username
NA
NA
NA
['Bob']
['']
['']
['Meril']
['Aston']
['Meril+Aston']

I need all the rows which has Bob, Meril and Aston or Meril+Aston or Bob+Aston….all combinations.

I tried

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df.username.str.extract(r"\['Bob.*|['Meril.*|['Aston.*")

It not working

>Solution :

Try this:

subset = df[df.username.astype('str').str.contains('Bob|Meril|Aston').fillna(False)]

Output:

>>> subset
          username
3          ['Bob']
6        ['Meril']
7        ['Aston']
8  ['Meril+Aston']
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading