0

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 ]]
1
  • The result will have 'float' indices (1.0) because the rest of the array is float. Mixing integer and float columns requires a more advanced structure. Commented Feb 7, 2017 at 0:04

1 Answer 1

3

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

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.