我试图了解如何使我的代码更简洁。我有下面的说法很好用:
cleaned = dc_listings['price'].str.replace(',', '').str.replace('$', '')
但是,当我尝试使用正则表达式时,如下所示,它不起作用:
cleaned = dc_listings['price'].str.replace(',|$', '')
清除后的变量仍然包含一些“ $”条目...我在做什么错?
谢谢!
需要$
通过进行转义\
,因为regex character
字符串的特殊结尾:
dc_listings = pd.DataFrame({'price':[',ll','j$,']})
cleaned = dc_listings['price'].str.replace(',|\$', '')
print (cleaned)
0 ll
1 j
Name: price, dtype: object
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句