0

I have a 2D numpy array that has a shape of (867, 43). My aim is to add an extra column (np.nan value) as the leading column to this array so that the shape becomes (867, 44).

An example would be:

# sub-section of array
>>> arr[:2, :5]

array([[-0.30368954,  2.8808107 ,  5.8833385 ,  8.6606045 , 11.242557  ],
       [-0.22719575,  3.0030012 ,  6.065371  ,  8.924864  , 11.561942  ]],
  dtype=float32)

would turn into:

# same sub-section
>>> f[:2,:5]

array([[        nan, -0.30368954,  2.8808107 ,  5.8833385 ,  8.6606045 ],
       [        nan, -0.22719575,  3.0030012 ,  6.065371  ,  8.924864  ]],
  dtype=float32)

i.e the values have been shifted right as the horizontal dimension has increased by one.

3 Answers 3

1

You can use np.hstack():

import numpy as np

my_arr = np.array([[-0.30368954,  2.8808107 ,  5.8833385 ,  8.6606045 , 11.242557  ],
                [-0.22719575,  3.0030012 ,  6.065371  ,  8.924864  , 11.561942  ]])

col = np.empty((my_arr.shape[0],1))
col[:] = np.nan
np.hstack((col, my_arr))

Returns:

[[        nan -0.30368954  2.8808107   5.8833385   8.6606045  11.242557  ]
 [        nan -0.22719575  3.0030012   6.065371    8.924864   11.561942  ]]
Sign up to request clarification or add additional context in comments.

Comments

1

Have a look at stack. Edit: clarification; I am making use of the broadcasting feature to insert a newaxis along the second dimension and hstack will then append the axis along the zero axis (default for hstack is rows or first dimension).

from numpy import array, hstack, nan, newaxis
a = array([[-0.30368954,  2.8808107 ,  5.8833385 ,  8.6606045 , 11.242557  ],
       [-0.22719575,  3.0030012 ,  6.065371  ,  8.924864  , 11.561942  ]],
  dtype=float32)

tmp = ones((a.shape[0])) * nan # create nan array
print(hstack((tmp[:, newaxis], a))) # append along zero axis 

Output:

[[        nan -0.30368954  2.88081074  5.88333845  8.66060448 11.24255657]
 [        nan -0.22719575  3.00300121  6.06537104  8.92486382 11.5619421 ]]

2 Comments

that works! However I'm sure there would be a way to do it which is a bitter clearer to whats going on... I'm trying to see if I can get numpy.insert to work in one line but I haven't got it to work yet..
addded some more info for you! And cleaned it up with the required nan array. Either using the newaxis or None. It is equivalent to specify a 1 in the shape of the ones method.
0

Using np.insert()

>>> import numpy as np
>>> arr
array([[-0.30368954,  2.8808107 ,  5.8833385 ,  8.6606045 , 11.242557  ],
       [-0.22719575,  3.0030012 ,  6.065371  ,  8.924864  , 11.561942  ]],

>>> arr = np.insert(arr, 0, np.nan, axis=0)
>>> arr
array([[        nan, -0.30368954,  2.8808107 ,  5.8833385 ,  8.6606045 , 11.242557  ],
       [        nan, -0.22719575,  3.0030012 ,  6.065371  ,  8.924864  , 11.561942  ]],

2 Comments

Nice! Didn't know about insert. Normally use stack for all this stuff. Learning something new everyday
No problem! Your stack example was going to be my fallback option. Cheers for the help!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.