Reputation: 3353
I have a dataset read from a .csv
file which is a 2d array.
Now I want to slice the dataset and get the first element of every list in it.
import numpy as np
rawData = []
with open(path) as file:
lines = csv.reader(file)
for line in lines:
rawData.append(line)
dataSet = rawData[0:10] # as the whole dataset is too large, I get the first ten rows to test
np.array(dataSet)
labels = dataSet[:,0]
If I run:
print np.shape(dataSet)
>>>(10, 785)
I've referred How to slice a 2D Python Array. However I still got this error.
Upvotes: 2
Views: 5246
Reputation: 6834
Syntax is wrong in the line: labels = dataSet[:,0]
.
In your code dataSet
is a python list and you are trying to access its key by using a python tuple ,0
; the comma defines a tuple in python.
You should use integers as the error message suggests.
To solve this, convert dataSet
to a numpy array like this: numpy.array(dataSet)
.
Upvotes: 1
Reputation: 3353
Thanks to @jonrsharpe .
np.array(dataSet)
just create a new numpy array but I didn't assign it to dataSet
.
It should be
result = np.array(dataSet)
Upvotes: 1