RaduS
RaduS

Reputation: 2555

numpy: get indices value in a separate column

How to get indices value in a separate column as effienctly as possible? i know how to do this in a loop, but i wonder what other ways there are?

from this ndarray

[[ 0.71587892  0.72278279 ]
 [ 0.72225173  0.73340414 ]
 [ 0.7259692   0.72862454 ]]

to this

[[0   0.71587892  0.72278279 ]
 [1   0.72225173  0.73340414 ]
 [2   0.7259692   0.72862454 ]]

Upvotes: 0

Views: 42

Answers (1)

yogabonito
yogabonito

Reputation: 657

How about

np.column_stack((np.arange(len(a)), a))

where a is your initial array?

Take a look at the following IPython-session:

In [1]: import numpy as np

In [2]: a = np.array([[0.71587892, 0.72278279],
   ...:  [ 0.72225173, 0.73340414],
   ...:  [ 0.7259692, 0.72862454]])

In [3]: np.column_stack((np.arange(len(a)), a))
Out[3]: 
array([[ 0.        ,  0.71587892,  0.72278279],
       [ 1.        ,  0.72225173,  0.73340414],
       [ 2.        ,  0.7259692 ,  0.72862454]])

Upvotes: 3

Related Questions