Home > Software design >  Replace only replacing the 1st argument
Replace only replacing the 1st argument

Time:01-03

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?

CodePudding user response:

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

CodePudding user response:

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
  • Related