Find Rows Where Column Value Contain in a String

Advertisements

Example:

data='findme, ok'
df = pd.DataFrame([x.split(',') for x in data.split('\n')])
df.columns = ['keyword', 'result']
keywords = df['keyword'].tolist()
test1='maybe you canfindme'.lower()

How can I convert the below syntax to dataframe syntax?

for keyword in df['keyword'].tolist():
    if keyword in test1:
        print(keyword)

Of course this is wrong, but I am looking for something like this:

filter_result = df[df['keyword'] in test1]

>Solution :

One way to do that is to use the map method.

See Map method documentation

In your case:

df[df['keyword'].map(lambda k: k in test1)==True]

Leave a ReplyCancel reply