David Leslie
David Leslie

Reputation: 43

how to remove multiple characters from dataframe

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!

Upvotes: 2

Views: 4710

Answers (1)

jezrael
jezrael

Reputation: 863541

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

Upvotes: 3

Related Questions