python - 通过 np.char.find 比较 pandas 数据帧的两列给出 TypeError : string operation on non-string array

标签 python pandas numpy

我想比较两个系列的字符串,看看一个是否包含另一个元素。

我首先尝试使用 apply,但它很慢:

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

它似乎适用于以下代码:

x=np.array(['one','two'])
y=np.array(['sdf','x y two'])

np.char.find(y,x)
array([-1,  4])

但是如果我有一个数据框,我会得到一个错误:

np.char.find(df.s2.values,df.s1.values)
TypeError: string operation on non-string array

有人可以建议解决方案吗?

最佳答案

numpy.core 中使用 find 并添加 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])

关于python - 通过 np.char.find 比较 pandas 数据帧的两列给出 TypeError : string operation on non-string array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53012298/

相关文章:

arrays - numpy 数组的最大大小是多少?

python - 如何在 numpy 数组中找到最大非无穷大值的索引?

python - numpy.ndarray.tofile 在 x 个条目后带有分界线?

Python 异步网络服务调用

python - 在 matplotlib Python 中设置不同的条形颜色

php - RSA:在Python和PHP中从n和e生成公钥给我两个不同的公钥

python - Pandas distinct 算作一个 DataFrame

python - 将 DataFrame 拆分为两个 DataFrame 并过滤这两个 DataFrame 以获得相同的维度

python - 如何更改绘图的大小并防止 y 轴重叠?

python - SciPy - 子类化 rv_continuous 时出现 'Object too deep for desired array'