0
votes

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?

3
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) - roganjosh
This is the most common error I receive: data type "" not understood - Dyland
thy this line in your method: myArray = np.array((splitDocument[0], splitDocument[1])) - n1tk
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)? - Dyland
if you can provide a "masked" data I can look into ... - n1tk

3 Answers

1
votes

Try:

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

res = data[:,0:2]
1
votes

You can also try:

 import numpy as np
 d = np.loadtxt('a.csv', delimiter=',')
 x = d[:,0]
 y = d[:,1]
0
votes
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.