0

I have a 2D numpy array of :

[[9.29526424407959, -3.68755626678467],
 [9.7620153427124, -2.16865086555481], 
 [9.9980001449585, 0.199986666440964], 
 [9.95050621032715, 0.993697226047516], 
 [9.84010124206543, 1.78112530708313], 
 [9.43374633789063, 3.31729197502136], 
 [8.7891960144043, 4.76969909667969], 
 [8.38245868682861, 5.4529242515564],
 [7.41290092468262, 6.71184778213501],
 [6.85620975494385, 7.27958679199219],
 [5.61658048629761, 8.27369403839111],
 [4.23513603210449, 9.05889701843262],
 [3.50201725959778, 9.36674308776855]]

I would need to add numbers at the start of it making it look like this.

[[1 ,9.29526424407959, -3.68755626678467],
 [2, 9.7620153427124, -2.16865086555481], 
 [3, 9.9980001449585, 0.199986666440964], 
 [4, 9.95050621032715, 0.993697226047516], 
 [5, 9.84010124206543, 1.78112530708313], 
 [6, 9.43374633789063, 3.31729197502136], 
 [7, 8.7891960144043, 4.76969909667969], 
 .
 .
 .

I've tried converting it to list and adding numbers but it keeps coming out wrong.

Any suggestions?

1

2 Answers 2

1
your_array = [[9.29526424407959, -3.68755626678467],
 [9.7620153427124, -2.16865086555481], 
 [9.9980001449585, 0.199986666440964], 
 [9.95050621032715, 0.993697226047516], 
 [9.84010124206543, 1.78112530708313], 
 [9.43374633789063, 3.31729197502136], 
 [8.7891960144043, 4.76969909667969], 
 [8.38245868682861, 5.4529242515564],
 [7.41290092468262, 6.71184778213501],
 [6.85620975494385, 7.27958679199219],
 [5.61658048629761, 8.27369403839111],
 [4.23513603210449, 9.05889701843262],
 [3.50201725959778, 9.36674308776855]]

your_array = np.array(your_array)
index = np.array(range(1,len(your_array)+1))
np.concatenate((index.reshape(-1,1),your_array),axis=-1)

output:

array([[ 1.        ,  9.29526424, -3.68755627],
       [ 2.        ,  9.76201534, -2.16865087],
       [ 3.        ,  9.99800014,  0.19998667],
       [ 4.        ,  9.95050621,  0.99369723],
       [ 5.        ,  9.84010124,  1.78112531],
       [ 6.        ,  9.43374634,  3.31729198],
       [ 7.        ,  8.78919601,  4.7696991 ],
       [ 8.        ,  8.38245869,  5.45292425],
       [ 9.        ,  7.41290092,  6.71184778],
       [10.        ,  6.85620975,  7.27958679],
       [11.        ,  5.61658049,  8.27369404],
       [12.        ,  4.23513603,  9.05889702],
       [13.        ,  3.50201726,  9.36674309]])
Sign up to request clarification or add additional context in comments.

Comments

0

You can use np.insert to append value to your numpy array.

 import numpy as np

 a = [[9.29526424407959, -3.68755626678467],
      [9.7620153427124, -2.16865086555481], 
      [9.9980001449585, 0.199986666440964], 
      [9.95050621032715, 0.993697226047516], 
      [9.84010124206543, 1.78112530708313], 
      [9.43374633789063, 3.31729197502136], 
      [8.7891960144043, 4.76969909667969], 
      [8.38245868682861, 5.4529242515564],
      [7.41290092468262, 6.71184778213501],
      [6.85620975494385, 7.27958679199219],
      [5.61658048629761, 8.27369403839111],
      [4.23513603210449, 9.05889701843262],
      [3.50201725959778, 9.36674308776855]]

 index = 0
 values = range(1, len(a)+1)
 b = np.insert(a, index, values, axis=1) 

 >>> array([[ 1.        ,  9.29526424, -3.68755627],
            [ 2.        ,  9.76201534, -2.16865087],
            [ 3.        ,  9.99800014,  0.19998667],
            [ 4.        ,  9.95050621,  0.99369723],
            [ 5.        ,  9.84010124,  1.78112531],
            [ 6.        ,  9.43374634,  3.31729198],
            [ 7.        ,  8.78919601,  4.7696991 ],
            [ 8.        ,  8.38245869,  5.45292425],
            [ 9.        ,  7.41290092,  6.71184778],
            [10.        ,  6.85620975,  7.27958679],
            [11.        ,  5.61658049,  8.27369404],
            [12.        ,  4.23513603,  9.05889702],
            [13.        ,  3.50201726,  9.36674309]])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.