Replace comma and dot in Pandas

Pratik Manghwani picture Pratik Manghwani · May 26, 2019 · Viewed 17.9k times · Source

I have a Column with data like 3.4500,00 EUR. Now I want to compare this with another column having float numbers like 4000.00. How do i take this string, remove the EUR and replace comma with decimal and then convert into float to compare.

Answer

Erfan picture Erfan · May 26, 2019

You can use regular expressions to make your conditions general that would work in all cases:

# Make example dataframe for showing answer
df = pd.DataFrame({'Value':['3.4500,00 EUR', '88.782,21 DOLLAR']})

              Value
0     3.4500,00 EUR
1  88.782,21 DOLLAR

Use str.replace with regular expression:

df['Value'].str.replace('[A-Za-z]', '').str.replace(',', '.').astype(float)

0    34500.00
1    88782.21
Name: Value, dtype: float64

Explanation:

  • str.replace('[A-Za-z\.]', '') removes all alphabetic characters and dots.
  • str.replace(',', '.') replaces the comma for a dot
  • astype(float) converts it from object (string) type to float