Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import csv file as numpy.array in python? [duplicate]

Tags:

python

csv

numpy

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])
like image 586
user3692521 Avatar asked Sep 02 '14 01:09

user3692521


People also ask

How do I import a CSV file into a NumPy array in Python?

To read CSV data into a record in a Numpy array you can use the Numpy library genfromtxt() function, In this function's parameter, you need to set the delimiter to a comma. The genfromtxt() function is used quite frequently to load data from text files in Python.

How do I read a CSV file into an array?

We'll use fopen() and fgetcsv() to read the contents of a CSV file, and then we'll convert it into an array using the array_map() and str_getcsv() functions.

Does NumPy array make a copy?

Slicing an array does not make a copy, it just creates a new view on the existing array's data.


2 Answers

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.]) 
like image 59
Anoop Avatar answered Sep 22 '22 07:09

Anoop


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.

like image 27
Warren Weckesser Avatar answered Sep 19 '22 07:09

Warren Weckesser