0

I use pandas tu read a csv fill with numeric data like these:

-9999;-11,558;-0,692;0,005;0,001;-1
-9999;-3,32;1,054;5,317;0;-1
...

I use this line of code to import:

data = pd.read_csv(file, usecols = [0,1,2,3,4], sep = ";", header=None)

and the result is all my dataframe is fill by my numeric converted in string ex: '3,4'

I try adding ,dtype=float to my pd.read_csv line but I got this error:

ValueError: could not convert string to float: '5,04'

!!??

Jonathan Roy
  • 405
  • 1
  • 6
  • 18

3 Answers3

2

you should write 5.04, the decimal point is required, if it's not in the number it will count as a tuple

XxJames07-
  • 1,833
  • 1
  • 4
  • 17
2

the parameters decimal="," and thousands="." are described in the documentation. they could help

https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html

data = pd.read_csv(file, usecols = [0,1,2,3,4],delimiter=";", decimal=",",thousands=".", header=None)

propably solved in https://stackoverflow.com/a/70748012/6889858

1

The decimal point in your numbers is comma. You should pass decimal=',' as argument to pandas.read_csv().

From the docs:

decimalstr, default ‘.’

Character to recognize as decimal point (e.g. use ‘,’ for European data).
buran
  • 13,682
  • 10
  • 36
  • 61