Pandas DataFrame filter str column based on other column

93 views Asked by At

I want to filter a DataFrame based on whether the value of one string column is a substring of the value in another string column.

According to this 2-year-old post this can be done using apply like so:

df = pd.DataFrame({'FNAME': ['Max', 'Tobi'], 'LNAME': ['Foo', 'Tobiwan']})

df.loc[ df.apply(lambda row: row.FNAME in row.LNAME, axis=1) ]

  FNAME    LNAME
1  Tobi  Tobiwan

I was wondering if there is some built-in vectorized way of doing this?

1

There are 1 answers

1
BENY On BEST ANSWER

Using replace

df[df.LNAME.replace(regex=r'(?i)'+ df.FNAME,value=True)==True]
  FNAME    LNAME
1  Tobi  Tobiwan