0

I am attempting to open a CSV file that contains 4 columns with about 100 rows. I want to have a 2D numpy array, that sets the first column as the x-coordinates and sets the second column as the y-coordinates.

import numpy as np

dataDocument = open("data.csv")
headers = dataDoument.readline()

def generateArray(dataDocument):
    for numbers in dataDocument:
        splitDocument = numbers.strip().split(",")
        myArray = np.array(splitDocument[0], splitDocument[1])
        
        return myArray 
       
print(generateArray(dataDocument))

I keep getting various error messages, the most common being 'data type "" not understood.' Any suggestions on where my logic error/general code error exists?

5
  • You need to be specific on the errors here. You may be able to use numpy directly or at leave use the CSV module to read the file. From what I can see, though, the current approach probably shouldn't error (The return in the for loop is a definite issue, though) Commented Apr 2, 2019 at 21:11
  • This is the most common error I receive: data type "" not understood Commented Apr 2, 2019 at 21:24
  • thy this line in your method: myArray = np.array((splitDocument[0], splitDocument[1])) Commented Apr 2, 2019 at 21:53
  • Seems to solve the issue, thanks very much! How do I edit my code to get it to return multiple array's (one for each row in the code)? Commented Apr 2, 2019 at 21:59
  • if you can provide a "masked" data I can look into ... Commented Apr 2, 2019 at 22:02

3 Answers 3

1

Try:

from numpy import genfromtxt    
data = genfromtxt('data.csv', delimiter=',')

res = data[:,0:2]
Sign up to request clarification or add additional context in comments.

1 Comment

The question suggests that it's comma-delimited, not tab-delimited
1

You can also try:

 import numpy as np
 d = np.loadtxt('a.csv', delimiter=',')
 x = d[:,0]
 y = d[:,1]

Comments

0
replace: myArray = np.array(splitDocument[0], splitDocument[1])
with: myArray = np.array((splitDocument[0], splitDocument[1])) 
in your method and should solve the issue.

Problem is due that you are passing the splitDocument[1]to your np.array as dtype parameter.

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.