I wish to compare two series of strings to find if one contains the other element-wise.
I first tried with apply, but it is slow:
cols = ['s1','s2']
list_of_series = [pd.Series(['one','sdf'],index=cols), pd.Series(['two','x y two'],index=cols)]
df = pd.DataFrame(list_of_series, columns=cols)
df
s1 s2
0 one sdf
1 two x y two
df.apply(lambda row: row['s1'] in row['s2'], axis=1)
0 False
1 True
dtype: bool
It seems to work with the following code:
x=np.array(['one','two'])
y=np.array(['sdf','x y two'])
np.char.find(y,x)
array([-1, 4])
but if I have a dataframe, I get an error:
np.char.find(df.s2.values,df.s1.values)
TypeError: string operation on non-string array
Can someone advise a solution?
Using find
from numpy.core
and add astype
str
from numpy.core.defchararray import find
find(df.s2.values.astype(str),df.s1.values.astype(str))!=-1
Out[430]: array([False, True])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With