1

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 :)

3
  • 1
    Data is a list not a numpy array Commented Dec 14, 2022 at 10:44
  • 1
    You have a list of lists, not a 2D array. Try Data[:][0] or convert to np.array(Data) and then use commas Commented Dec 14, 2022 at 10:45
  • Is there a possibility to only get the index. Because now I get (array([2], dtype=int64),). But I need the index for data_origin_y = data[0:np.where(data[:,0]-data[0,0] == 0.1), 1] Commented Dec 14, 2022 at 11:27

2 Answers 2

1

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]),)
Sign up to request clarification or add additional context in comments.

Comments

1

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]),)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.