我正在从数据帧中删除与特定列下的$1
完全匹配的行。
我是这样做的
apts = apts[~apts.Price.str.contains('$1')]
这不会删除任何行。但是我知道我和$1
在价格列下面有一些行。
例如,这将返回true:
if str(apts.ix[8193]['Price']) == '$1':
print('True')
知道这是怎么回事吗?
发布于 2017-08-03 05:08:43
$
是一个具有特殊含义的元字符。你需要对它进行转义,以符合字面意思。从here可以看出它依赖于正则表达式。
apts = apts[~apts.Price.str.contains('\$1')]
发布于 2017-08-03 05:08:05
它看起来像$
contains
方法除了一个正则表达式,在这种情况下,Pandas用来表示行的结束。您可能希望使用\
对$
进行转义,因此您的代码应该是apts = apts[~apts.Price.str.contains('\$1')]
发布于 2017-08-03 05:35:23
除了用'\$'
转义'$'
之外,重要的是要知道可以用regex=False
关闭正则表达式的使用
考虑一下示例数据帧apts
apts = pd.DataFrame(dict(Price=['2,000', '$1,000', '1000', '$14']))
Price
0 2,000
1 $1,000
2 1000
3 $14
然后使用regex=False
apts[apts.Price.str.contains('$', regex=False)]
Price
0 2,000
2 1000
https://stackoverflow.com/questions/45470972
复制相似问题