Rajarshi Das
Rajarshi Das

Reputation: 12320

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

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

It not working

Upvotes: 0

Views: 36

Answers (1)

user17242583
user17242583

Reputation:

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']

Upvotes: 1

Related Questions