熊猫数据框检查列是否包含另一列中存在的字符串

时间:2020-06-26 21:34:40

标签: python pandas dataframe csv

我正在尝试学习数据帧,但是我仍然是一个初学者。 假设我有一个包含两列的DataFrame:

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

我想检查名称是否也是描述的一部分,如果是,则保留该行。如果不是,请删除该行。在这种情况下,它将删除第三行(JW Employee在某处)

2 个答案:

答案 0 :(得分:2)

尝试一下:

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

答案 1 :(得分:0)

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)