简体   繁体   中英

Pandas read_csv import my numeric data as str?

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'

????

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

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 (eg use ',' for European data).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM