Reputation: 61
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)
Upvotes: 1
Views: 2581
Reputation: 26676
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)
Upvotes: 1
Reputation: 908
Try this:
df[df.apply(lambda x: x['Name'] in x['Description'], axis = 1)]
Upvotes: 2