I am having some difficulty with importing data from a .csv file. I am simply trying to import the data and print the max value. Here is my code:
>>> x, y = numpy.loadtxt('data.csv', delimiter=',', usecols=(4,5), unpack=True)
>>> print 'max =', max(x)
When I enter the above code, I get the following error message:
TypeError: 'numpy.float64' object is not iterable
I tried to change the data type using the dtype=int argument, but it threw the same error. Does anyone have any idea what could be the problem? Thanks in advance for your help!
The output of
loadtxt()is unfortunately a bit inconistent: If there is only one line in your file,xandywill be scalars, but for more than one line, they will be arrays. The Python built-inmax()only works for iterables, so it only works in the latter case.Using the Python built-in
max()function instead ofnumpy.max()is inefficient for NumPy arrays anyway. So a solution is to useor
in the second line.