What is the difference between indexing a 2D array row/col with
[row, col] in numpy/pandas? Is there any implications of using either of these two?
import numpy as np arr = np.array([[1, 2], [3, 4]]) print(arr) print(arr[1, 0])
For single elements indexing as in your example, the result is indeed the same. Although as stated in the docs:
So note that
x[0,2] = xthough the second case is more inefficient as a new temporary array is created after the first index that is subsequently indexed by 2.
In this case, not only that double-indexing is less efficient – it simply gives different results. Let’s look at an example:
>>> arr = np.array([[1, 2], [3, 4], [5, 6]]) >>> arr[1:] [3 4] >>> arr[1:, 0] [3 5]
In the first case, we create a new array after the first index which is all rows from index 1 onwards:
>>> arr[1:] [[3 4] [5 6]]
Then we simply take the first element of that new array which is
In the second case, we use numpy indexing which doesn’t index the elements but indexes the dimensions. So instead of taking the first row, it is actually taking the first column –