这是我的数据,其中包含数字和字符串。
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: []
任何人都可以帮助我吗?
答案 0 :(得分:2)
str.match
从头开始匹配。但是,您的$
模式只会在最后找到。
修复需要修改模式或更改功能。
选项1
具有修改模式的str.match
(最后匹配\$
) -
df2[df2.A.str.match('.*\$$')]
A
0 1,008$
1 4,000$
2 6,000$
3 10,00$
4 8,00$
如果您想具体说明匹配的内容,则只能匹配数字和逗号 -
df2[df2.A.str.match('[\d,]+\$$')]
A
0 1,008$
1 4,000$
2 6,000$
3 10,00$
4 8,00$
请注意,这不会考虑列中的无效条目(只要字符串中包含这些字符,它们就会匹配,并由$
终止。)
选项2
str.contains
df2[df2.A.str.contains('\$$')]
A
0 1,008$
1 4,000$
2 6,000$
3 10,00$
4 8,00$
答案 1 :(得分:2)
使用Numpy的# 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$
旧答案
<string name =ca-app-pub-2345567777/6578687568"></string>