31

say i have a csv file.csv in this format:

dfaefew,432,1
vzcxvvz,300,1
ewrwefd,432,0

how to import the second column as a numpy.array and the third column as another one like this:

second = np.array([432, 300, 432])
third = np.array([1, 1, 0])

I am using python2.7 in Ubuntu.

Thx ahead!

user3692521
  • 2,373
  • 4
  • 26
  • 31

2 Answers2

53

numpy.genfromtxt() is the best thing to use here

import numpy as np
csv = np.genfromtxt ('file.csv', delimiter=",")
second = csv[:,1]
third = csv[:,2]

>>> second
Out[1]: array([ 432.,  300.,  432.])

>>> third
Out[2]: array([ 1.,  1.,  0.])
Anoop
  • 5,170
  • 7
  • 34
  • 49
  • 2
    genfromtxt works better than loadtxt in my use case and I had to add dtype=None since my data had a mix of data types that I was reading. Just FYI.... – Nikhil Gupta Sep 21 '19 at 10:59
13

You can use numpy.loadtxt:

In [15]: !cat data.csv
dfaefew,432,1
vzcxvvz,300,1
ewrwefd,432,0

In [16]: second, third = loadtxt('data.csv', delimiter=',', usecols=(1,2), unpack=True, dtype=int)

In [17]: second
Out[17]: array([432, 300, 432])

In [18]: third
Out[18]: array([1, 1, 0])

Or numpy.genfromtxt

In [19]: second, third = genfromtxt('data.csv', delimiter=',', usecols=(1,2), unpack=True, dtype=None)

The only change in the arguments is that I used dtype=None, which tells genfromtxt to infer the data type from the values that it finds in the file.

Warren Weckesser
  • 102,583
  • 19
  • 173
  • 194