winterdiablo
winterdiablo

Reputation: 25

Replace only replacing the 1st argument

I have the following code:

df['Price'] = df['Price'].replace(regex={'$': 1, '$$': 2, '$$$': 3})

df['Price'].fillna(0)

but even if a row had "$$" or "$$$" it still replaces it with a 1.0.

How can I make it appropriately replace $ with 1, $$ with 2, and $$$ with 3?

Upvotes: 0

Views: 62

Answers (2)

mozway
mozway

Reputation: 261890

Assuming you really want to use regex matching and not a simple map, you need to fix 2 things.

1- escape $ which means end of string in regex

2- put the patterns in reverse order of length in the dictionary to have $$$ be evaluated before $$ and before $ ({r'\$\$\$': 3, r'\$\$': 2, r'\$': 1})

Example:

df = pd.DataFrame({'price': ['abc $', 'def $$', '$$$']})
df['new'] = df['price'].replace(regex={r'\$\$\$': 3, r'\$\$': 2, r'\$': 1})

Output:

    price  new
0   abc $    1
1  def $$    2
2     $$$    3

Upvotes: 0

Leo Liu
Leo Liu

Reputation: 730

df.Price.map({'$': 1, '$$': 2, '$$$': 3})

Upvotes: 3

Related Questions