1

While reading from csv I have ValueError: could not convert string to float. It begins from readng the file, after I give names to columns and remove NaNs.

from pandas import read_csv
df = read_csv("propositio/data.csv", header=None, 
    sep=';', decimal=',')
cols = ['col1', 'col2', 'col3', 'col4']
df.columns = cols
df.dropna(inplace=True)

Here comes an error. I want to convert non-null object into floats:

df = df.astype(float)

ValueError: could not convert string to float: '4,6'

How can I fix it?

mohamoud wueza
  • 13
  • 1
  • 1
  • 3
  • 4
    `df = df.str.replace(",", ".").astype(float)` ? – Rakesh Jul 23 '18 at 13:23
  • 1
    Not a python expert, but if I read [Rakesh](https://stackoverflow.com/users/532312/rakesh)'s suggestion correctly, it matches my assumption - unless your locale handles it, Python is likely expecting the American standard of a decimal point dot rather than a comma. Try that, possibly with Rakesh's code. – Paul Hodges Jul 23 '18 at 13:30
  • Possible duplicate of [Convert commas to dots within a Dataframe](https://stackoverflow.com/questions/31700691/convert-commas-to-dots-within-a-dataframe) – Thomas Weller Jul 23 '18 at 13:32

1 Answers1

6

Use str.replace to replace comma with dot

Ex:

import pandas as pd

df = pd.DataFrame({"A": ['4,6', "5.6", '6,6']})
df = df["A"].str.replace(',', ".").astype(float)
print(df)

Output:

0    4.6
1    5.6
2    6.6
Name: A, dtype: float64
Rakesh
  • 81,458
  • 17
  • 76
  • 113