vinc00
vinc00

Reputation: 55

numpy where function TypeError

I need to find the Index of an 2D array, where the first Column equals 0.1. So something like this:

Data = [[0, 1], [0.075, 1], [0.1, 1], [0.11, 1], [0.125, 1]]
print(np.where(Data[:,0] == 0.1))

But then I get the following Error:

TypeError: list indices must be integers or slices, not tuple

Would be great if somebody could help me :)

Upvotes: 1

Views: 86

Answers (2)

Dani Mesejo
Dani Mesejo

Reputation: 61910

The error:

TypeError: list indices must be integers or slices, not tuple

is because Data is a list not a numpy array, notice that it says tuple because you are passing the tuple (:, 0) to the __getitem__ method of Data. For more info see this, to fix it just do:

import numpy as np

Data = np.array([[0, 1], [0.075, 1], [0.1, 1], [0.11, 1], [0.125, 1]])
print(np.where(Data[:, 0] == 0.1))

Output

(array([2]),)

Upvotes: 1

Yash Mehta
Yash Mehta

Reputation: 2006

just a typo.. you not initialised Data into an array it is in list form

Code:-

import numpy as np
Data = np.array([[0, 1], [0.075, 1], [0.1, 1], [0.11, 1], [0.125, 1]])
print(np.where(Data[:,0] == 0.1))

Output:-

(array([2]),)

Upvotes: 1

Related Questions