python - 我没有得到匹配的字符串

标签 python regex string pandas

这是我的数据,包含数字和字符串。

df2 = pd.DataFrame({'A': ['1,008$','4,000$','6,000$','10,00$','8,00$','45€','45€']})
df2 = pd.DataFrame(df2, columns = ['A'])
vv=df2[df2['A'].str.match('$')]

我想要这样的输出。

0  1,008$
1  4,000$
2  6,000$
3  10,00$
4   8,00$

但我得到了这个输出:

Out[144]: 
Empty DataFrame
Columns: [A]
Index: []

谁能帮帮我?

最佳答案

使用 Numpy 的 defchararray 模块的一种有点冗长的方式。
我一直想对此给予一些关注。

# Using @cᴏʟᴅsᴘᴇᴇᴅ's suggestion
# Same function as below but shorter namespace path

df2[np.char.find(df2.A.values.astype(str), '$') >= 0]

旧答案

from numpy.core.defchararray import find

df2[find(df2.A.values.astype(str), '$') >= 0]

        A
0  1,008$
1  4,000$
2  6,000$
3  10,00$
4   8,00$

关于python - 我没有得到匹配的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47974814/

相关文章:

javascript - javascript中分配字符串的区别

python - 未找到 substr 时表达式评估为 None

可截断循环缓冲区的Python快速数据结构

regex - 当 sqlite 返回多行时,如何获取结果列表?

javascript - 如何使用javascript替换字符串中最后一次出现的字符

c - 在 char* 数组中存储字符串时出错

python - 如何拟合一些系数受限的多项式?

python - 在 colorbar matplotlib 上记录标签

python - 如何阻止 Matplotlib 重复颜色?

regex - 需要找到所有 2 个大写单词的集合