我正在尝试学习数据帧,但是我仍然是一个初学者。 假设我有一个包含两列的DataFrame:
Name Description
Am Owner of Am
BQ Employee at BQ
JW Employee somewhere
我想检查名称是否也是描述的一部分,如果是,则保留该行。如果不是,请删除该行。在这种情况下,它将删除第三行(JW Employee在某处)
答案 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)