1

When I filter the data frame using the the code below, it works fine

my_df.loc[lambda x:x["name"]=="space"]

When I filter using the following code, it gives an error

my_df.loc[lambda x: difflib.SequenceMatcher(None,"email",x["name"]).ratio()>0.8]

I want to filter using SequenceMatcher and maybe using more complex condition than the above one

Here is the full code:

import pandas as pd
import difflib
my_df=pd.DataFrame({"name":["space","mapp","eemail","daata"],"id":[9,12,13,14]})
my_df.loc[lambda x:x["name"]=="space"] #this line works
my_df.loc[lambda x: difflib.SequenceMatcher(None,"email",x["name"]).ratio()>0.8] #this doesn't

1 Answer 1

2

Try the following:

my_df.loc[my_df['name'].apply(lambda x: difflib.SequenceMatcher(None,"email",x).ratio()) > 0.8]

Output:

    id  name
2   13  eemail
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.