'importing data from csv - could not convert string to float

I am having difficulties importing some data from a csv file.

Input from csv file (extract):

  Speed;A
  [rpm];[N.m]
  700;-72,556
  800;-58,9103
  900;-73,1678
  1000;-78,2272

Code:

import pandas as pd
inT = "test.csv"
df = pd.read_csv(inT, sep = ";", decimal = ',')
print(df)
df = df.loc[1:]
df=df.astype(float)
ax = df.plot(x='Speed')

Error: The decimal replace does not work and the following python error occurs

could not convert string to float: '-72,556'

Desired Output:

   Speed         A
0  700.0   -72.556
1  800.0  -58.9103
2  900.0  -73.1678
3  1000.0  -78.2272


Solution 1:[1]

Use skiprows=[1] in read_csv to skip the row 1, the conversion to float should be automatic:

df = pd.read_csv('test.csv', sep = ';', decimal = ',', skiprows=[1])

output:

print(df)
     Speed        A
0      700 -72.5560
1      800 -58.9103
2      900 -73.1678
3     1000 -78.2272

print(df.dtypes)
  Speed      int64
A          float64
dtype: object

Why your code did not work

When reading the "[rpm];[N.m]" line, the csv parsers determines that the column(s) are strings, not floats. So the decimal specifier is simply ignored and the -72,556 values remain as string, with the comma.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mozway