Zygi
Zygi

Reputation: 55

Could not convert string to float: '2.648.142' (Panda)

I am trying to convert a single column, taken from an html import, from string into float. The column(Price) includes a currency sign, that i tried to replace with ''.

import pandas as pd

Cars = {'Brand': ['Honda Civic','Toyota Corolla','Ford Focus','Audi A4'],
        'Price': [2.648.142€,2500000€,2700000€,35000€]
        }

df = pd.DataFrame(cars, columns = ['Brand', 'Price'])


df['Price']=df['Price'].replace('€','', regex=True)

df['Price']= df['Price'].astype(float)

print (df)

error:

ValueError: could not convert string to float: '2.648.142'

If i use

pd.to_numeric(df, errors='coerce')

error:

TypeError: arg must be a list, tuple, 1-d array, or Series

The output only includes price < 1.000.000, therefore i assume that the double dot creates the problem.

Upvotes: 0

Views: 400

Answers (1)

jezrael
jezrael

Reputation: 862511

You can also replace . to empty string, because special regex character is escaped by \:

df['Price'] = df['Price'].replace('[€\.]','', regex=True).astype(float)
print (df)
            Brand      Price
0     Honda Civic  2648142.0
1  Toyota Corolla  2500000.0
2      Ford Focus  2700000.0
3         Audi A4    35000.0

Upvotes: 1

Related Questions