pandas dataframe check if column contains string that exists in another column

房东的猫 提交于 2021-01-05 09:14:06

问题


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:


Try this:

df[df.apply(lambda x: x['Name'] in x['Description'], axis = 1)]



回答2:


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)


来源:https://stackoverflow.com/questions/62603123/pandas-dataframe-check-if-column-contains-string-that-exists-in-another-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!