Replace comma and dot in Pandas

21,810

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
Share:
21,810
Admin
Author by

Admin

Updated on July 05, 2022

Comments

  • Admin
    Admin almost 2 years

    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.