2

I'm trying to understand how to make my code more concise. I have the following statement which works fine:

cleaned = dc_listings['price'].str.replace(',', '').str.replace('$', '')

However, when I try using a regex, as in the below, it does not work:

cleaned = dc_listings['price'].str.replace(',|$', '')

The cleaned variable still contains some '$' entries... What am I doing wrong?

Thanks!

0

1 Answer 1

3

Need escape $ by \, because special regex character - end of string:

dc_listings = pd.DataFrame({'price':[',ll','j$,']})
cleaned = dc_listings['price'].str.replace(',|\$', '')
print (cleaned)
0    ll
1     j
Name: price, dtype: object
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.