Search code examples
pythonindex-error

"Too many indices for array" When trying to define a column of values


data = np.genfromtxt('radondecay.csv', delimiter=',', skip_header=0)
print('radon decay =')
print(data)

radon decay = [ 11. 12. 12. 23. 17. 20. 10. 17. 12. 15. 20. 14. 21. 19.

I copied the first line of data onto here, but this goes on for many more lines. The original file is just one column of values.

count = data[:,0]
print("counts =", count)

IndexError                                Traceback (most recent call last)
<ipython-input-58-8a5c8f0bfbad> in <module>
----> 1 count = data[:,0]
      2 print("counts =", count)

IndexError: too many indices for array

I looked at other examples of this on the site, but I'm not too good with programming in general so I can't understand the context of most other peoples code.


Solution

  • Your data has one dimension (vector as opposed to matrix).

    1D is accessed by one index or slice. i.e. data[1] or data[2:5]. You can verify it by data.shape