1

I am trying to learn about Dataframes but I'm still a beginner. Let's say I have a DataFrame that contains two columns:

Name       Description
Am         Owner of Am
BQ         Employee at BQ  
JW         Employee somewhere

I want to check if the name is also a part of the description, and if so keep the row. If it's not, delete the row. In this case, it will delete the 3rd row (JW Employee somewhere)

1
  • Look at the timing with the answers. You can test it yourself. speed is important Commented Jun 27, 2020 at 0:03

2 Answers 2

2

Try this:

df[df.apply(lambda x: x['Name'] in x['Description'], axis = 1)]
Sign up to request clarification or add additional context in comments.

Comments

1
s='|'.join(df.Name)#Join the search words into a pattern
df=df[df.Description.str.contains(s)]#Mask using boolean select
print (df)

 Name     Description
0   Am     Owner of Am
1   BQ  Employee at BQ


%%timeit
s='|'.join(df.Name)
df[df.Description.str.contains(s)]
537 µs ± 2.37 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

%timeit df[df.apply(lambda x: x['Name'] in x['Description'], axis = 1)]
1.27 ms ± 3.22 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

2 Comments

This answer is faster and will serve you better in a large dataset
Good, but not 'None' - protected)

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.