Reputation: 1538
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?
Upvotes: 1
Views: 91
Reputation: 323316
Using replace
df[df.LNAME.replace(regex=r'(?i)'+ df.FNAME,value=True)==True]
FNAME LNAME
1 Tobi Tobiwan
Upvotes: 2