2

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
3
  • 1
    which pandas version are you using? Commented May 13, 2022 at 8:24
  • 2
    The values are loaded as strings due to the second header line. If you print(df.dtypes) you'll see that both series are object Commented May 13, 2022 at 8:25
  • @mozway: Using 1.3.4 Commented May 13, 2022 at 9:02

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.