"Got 1 columns instead of ..." error in numpy "Got 1 columns instead of ..." error in numpy numpy numpy

"Got 1 columns instead of ..." error in numpy


genfromtxt will give this error if the number of columns is unequal.

I can think of 3 ways around it:

1. Use the usecols parameter

np.genfromtxt('yourfile.txt',delimiter=',',usecols=np.arange(0,1434))

However - this may mean that you lose some data (where rows are longer than 1434 columns) - whether or not that matters is down to you.

2. Adjust your input data file so that it has an equal number of columns.

3. Use something other than genfromtxt:

.............like this


An exception is raised if an inconsistency is detected in the number of columns.A number of reasons and solutions are possible.

  1. Add invalid_raise = False to skip the offending lines.

    dataset = genfromtxt(open('data.csv','r'), delimiter='', invalid_raise = False)

  2. If your data contains Names, make sure that the field name doesn’t contain any space or invalid character, or that it does not correspond to the name of a standard attribute (like size or shape), which would confuse the interpreter.

  1. deletechars

    Gives a string combining all the characters that must be deleted from the name. By default, invalid characters are ~!@#$%^&*()-=+~\|]}[{';: /?.>,<.

  2. excludelist

    Gives a list of the names to exclude, such as return, file, print… If one of the input name is part of this list, an underscore character ('_') will be appended to it.

  3. case_sensitive

    Whether the names should be case-sensitive (case_sensitive=True), converted to upper case (case_sensitive=False or case_sensitive='upper') or to lower case (case_sensitive='lower').

data = np.genfromtxt("data.txt", dtype=None, names=True,\       deletechars="~!@#$%^&*()-=+~\|]}[{';: /?.>,<.", case_sensitive=True)

Reference: numpy.genfromtxt


You have too many columns in one of your rows. For example

>>> import numpy as np>>> from StringIO import StringIO>>> s = """... 1 2 3 4... 1 2 3 4 5... """>>> np.genfromtxt(StringIO(s),delimiter=" ")Traceback (most recent call last):  File "<stdin>", line 1, in <module>  File "/usr/lib64/python2.6/site-packages/numpy/lib/npyio.py", line 1654, in genfromtxt    raise ValueError(errmsg)ValueError: Some errors were detected !    Line #2 (got 5 columns instead of 4)