You need to add a slice to the index.

In:

in_array = np.asarray([[7, 40], [777, 440]])
for index in np.ndindex(in_array.shape[1:]):
    print "---"
    print index
    print in_array[:, index] # gives 2D array

index has values like (0,),(1,), i.e. tuples.

in_array[:,(1,)] is not the same as in_array[:,1]. To get the latter you need to use in_array[(slice(None),1)]. The slice must be part of the index tuple. We can do that by concatenating tuples.

in_array = np.asarray([[7, 40], [777, 440]])
for index in np.ndindex(in_array.shape[1:]):
    print "---"
    index = (slice(None),)+index
    print index
    print in_array[index]

printing:

---
(slice(None, None, None), 0)
[  7 777]
---
(slice(None, None, None), 1)
[ 40 440]

Same adjustment should work with the nD array case

Answer from hpaulj on Stack Overflow
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.ndindex.html
numpy.ndindex — NumPy v2.5 Manual
Given the shape of an array, an ndindex instance iterates over the N-dimensional index of the array.
numpy.where
Return elements chosen from x or y depending on condition · When only condition is provided, this function is a shorthand for np.asarray(condition).nonzero(). Using nonzero directly should be preferred, as it behaves correctly for subclasses. The rest of this documentation covers only the ...
Array creation routines
empty(shape[, dtype, order, device, like]) · Return a new array of given shape and type, without initializing entries
numpy.nonzero
Return the indices of the elements that are non-zero · Returns a tuple of arrays, one for each dimension of a, containing the indices of the non-zero elements in that dimension. The values in a are always tested and returned in row-major, C-style order
Array manipulation routines
copyto(dst, src[, casting, where]) · Copies values from one array to another, broadcasting as necessary
🌐
NumPy
numpy.org › doc › 1.14 › reference › generated › numpy.ndindex.html
numpy.ndindex — NumPy v1.14 Manual
Given the shape of an array, an ndindex instance iterates over the N-dimensional index of the array.
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.ndindex.html
numpy.ndindex — NumPy v2.1 Manual
Given the shape of an array, an ndindex instance iterates over the N-dimensional index of the array.
🌐
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.ndindex.html
numpy.ndindex — NumPy v2.3 Manual
Given the shape of an array, an ndindex instance iterates over the N-dimensional index of the array.
🌐
NumPy
numpy.org › doc › 1.20 › reference › generated › numpy.ndindex.html
numpy.ndindex — NumPy v1.20 Manual
Given the shape of an array, an ndindex instance iterates over the N-dimensional index of the array.
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.ndindex.html
numpy.ndindex — NumPy v2.6.dev0 Manual
Given the shape of an array, an ndindex instance iterates over the N-dimensional index of the array.
🌐
NumPy
numpy.org › doc › 1.22 › reference › generated › numpy.ndindex.html
numpy.ndindex — NumPy v1.22 Manual
Given the shape of an array, an ndindex instance iterates over the N-dimensional index of the array.
🌐
Quansight-labs
quansight-labs.github.io › ndindex
ndindex documentation
Additionally, the ndindex documentation contains an extensive guide to NumPy indexing, which goes over how all NumPy index types work, and should be useful to all users of NumPy even if you aren’t a user of ndindex.
Find elsewhere
🌐
SciPy
docs.scipy.org › doc › numpy-1.17.0 › reference › generated › numpy.ndindex.html
numpy.ndindex — NumPy v1.17 Manual
class numpy.ndindex(*shape)[source]¶ · An N-dimensional iterator object to index arrays. Given the shape of an array, an ndindex instance iterates over the N-dimensional index of the array. At each iteration a tuple of indices is returned, the last dimension is iterated over first.
🌐
NumPy
numpy.org › doc › 1.21 › reference › generated › numpy.ndindex.html
numpy.ndindex — NumPy v1.21 Manual
June 22, 2021 - Given the shape of an array, an ndindex instance iterates over the N-dimensional index of the array.
🌐
NumPy
numpy.org › doc › 2.4 › reference › generated › numpy.ndindex.html
numpy.ndindex — NumPy v2.4 Manual
Given the shape of an array, an ndindex instance iterates over the N-dimensional index of the array.
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.ndindex.html
numpy.ndindex — NumPy v2.2 Manual
Given the shape of an array, an ndindex instance iterates over the N-dimensional index of the array.
🌐
NumPy
numpy.org › doc › 1.26 › reference › generated › numpy.ndindex.html
numpy.ndindex — NumPy v1.26 Manual
Given the shape of an array, an ndindex instance iterates over the N-dimensional index of the array.
🌐
NumPy
numpy.org › doc › 2.0 › reference › generated › numpy.ndindex.html
numpy.ndindex — NumPy v2.0 Manual
Given the shape of an array, an ndindex instance iterates over the N-dimensional index of the array.
🌐
PyPI
pypi.org › project › ndindex
ndindex · PyPI
ndindex is a library that allows representing and manipulating objects that can be valid indices to numpy arrays, i.e., slices, integers, ellipses, None, integer and boolean arrays, and tuples thereof.
🌐
NumPy
numpy.org › doc › 1.19 › reference › generated › numpy.ndindex.html
numpy.ndindex — NumPy v1.19 Manual
June 29, 2020 - Given the shape of an array, an ndindex instance iterates over the N-dimensional index of the array.
🌐
NumPy
numpy.org › doc › 1.16 › reference › generated › numpy.ndindex.html
numpy.ndindex — NumPy v1.16 Manual
Given the shape of an array, an ndindex instance iterates over the N-dimensional index of the array.