0

I'm trying to import this data set via csv.

import numpy as np
import csv 

Snap1 = []
Snap2 = []

with open('BrownM.csv', 'r') as f:
    reader = csv.reader(f, delimiter=',')
    for row in reader:
        Snap1.append(row[0])
        Snap2.append(row[1])

Snap1 = np.array(Snap1)

This works fine, but trying to append 89 columns and converting those to arrays will be extremely time consuming.

Is their an easier method to my madness?

iron2man
  • 1,787
  • 5
  • 27
  • 39
  • 1
    Possible duplicate of [How to read csv into record array in numpy?](http://stackoverflow.com/questions/3518778/how-to-read-csv-into-record-array-in-numpy) – R Nar Jun 29 '16 at 14:56
  • @RNar, I just took a look at that. Trying that would return `nan` in my sets. Could it be the values in my original data file? – iron2man Jun 29 '16 at 15:03

1 Answers1

2

You could try using the genfromtxt function from numpy. By giving the delimiter as , it loads a csv file into a 2D array that you can slice into columns.

Like this:

import numpy as np

with open('BrownM.csv', 'r') as f:
    data = np.genfromtxt(f, delimiter=',')
Snap1 = data[:, 0]
Snap2 = data[:, 1]
eistaa
  • 86
  • 1
  • 7
  • @darthlazar, if you are struggling with `nan` in the parsed data, it could be that values are missing in the csv file. In that case adding `filling_values=0` would place `0` instead of `nan` in the arrays – eistaa Jun 29 '16 at 15:30