Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using genfromtxt to import csv data with missing values in numpy

I have a csv file that looks something like this (actual file has many more columns and rows):

1,2,3,4,5
6,7,8,9,10
11,12,13,14,15
16

Say the name of the file is info.csv If I try to import this using

data = numpy.genfromtxt('info.csv', delimiter = ',')

then I get the following error:

ValueError: Some errors were detected ! Line #4 (got 1 columns instead of 5)

If I use,

data = numpy.genfromtxt('info.csv', delimiter = ',', skip_footer = 1) 

both lines with data 16 and with data 11, 12, 13, 14, 15 are skipped. I don't understand why the line with 11, 12, 13, 14, 15 is being skipped. I would appreciate any help on how I can appropriately use the genfromtxt to import first three lines in the above file.

Thanks

like image 926
Curious2learn Avatar asked Sep 21 '10 14:09

Curious2learn


People also ask

What can be used to fill missing values in CSV with NumPy?

Using genfromtxt to import csv data with missing values in numpy.

How does NumPy Genfromtxt work?

genfromtxt() function. The genfromtxt() used to load data from a text file, with missing values handled as specified. Each line past the first skip_header lines is split at the delimiter character, and characters following the comments character are discarded.

How do I import a csv file into NumPy?

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 import Genfromtxt into Python?

For example, if we want to import only the first and the last columns, we can use usecols=(0, -1) : >>> data = u"1 2 3\n4 5 6" >>> np. genfromtxt(StringIO(data), usecols=(0, -1)) array([[1., 3.], [4., 6.]])


1 Answers

if you can ignore the 16 at the end of the file try using the

invalid_raise (bool, optional) parameter if set to False it ignores all incomplete lines without throwing an exception

see here (its the last parameter before the examples) http://docs.scipy.org/doc/numpy/reference/generated/numpy.genfromtxt.html

like image 176
Nikolaus Gradwohl Avatar answered Oct 15 '22 20:10

Nikolaus Gradwohl