0

I am using numpy to create an array out of a .csv file. This file's top row is made up of text, so I copied this command from the article I was using to leave it out when using float(I have left in the whole code for context. The command is the [1:] in the last line)

import numpy as np
import csv
with open("atest.csv", 'r') as f:
    wines = list(csv.reader(f, delimiter=";"))
    print(wines[:3])
wines_array = np.array(wines[1:], dtype=np.float)

However, I still get this error message:

    wines_array = np.array(wines[1:], dtype=np.float)
ValueError: could not convert string to float: 'fixed acidity'

This indicates that it has not left out the top row. Does anyone have any help?

EDIT

This is my .csv file:

"fixed acidity";"volatile acidity";"citric acid";"residual sugar";"chlorides";"free sulfur dioxide";"total sulfur dioxide";"density";"pH";"sulphates";"alcohol";"quality"
7.4;0.7;0;1.9;0.076;11;34;0.9978;3.51;0.56;9.4;5
7.8;0.88;0;2.6;0.098;25;67;0.9968;3.2;0.68;9.8;5

The first line is all one row in my file.

20
  • 1
    show your .csv. Commented Dec 29, 2017 at 13:32
  • 2
    ^ That, and you really should be using something like np.loadtxt or genfromtxt or pandas. Commented Dec 29, 2017 at 13:33
  • 1
    edit your question and add it there. Commented Dec 29, 2017 at 13:46
  • 1
    I have tested your code and it runs correctly. Commented Dec 29, 2017 at 14:01
  • 1
    python 3.6.4 - python 2.7.14 - numpy 1.13.3 Commented Dec 29, 2017 at 14:04

1 Answer 1

1

It turned out that python wasn't parsing the file properly, so I switched libraries to pandas and slightly modified and used the code suggested by the user COLDSPEED in the comments. The error appears to be individual to me.

Here is the line of code I used: import pandas as pd; v = pd.read_csv('atestred.csv', error_bad_lines=False).values

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

2 Comments

Make sure to add the code you used, so as to help other users with the same problem.
One sec @COLDSPEED

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.