Max Collier
Max Collier

Reputation: 691

Adding an extra in column into 2D numpy array python

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.

Upvotes: 0

Views: 242

Answers (3)

Max Collier
Max Collier

Reputation: 691

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  ]],

Upvotes: 0

cvanelteren
cvanelteren

Reputation: 1703

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 ]]

Upvotes: 1

rahlf23
rahlf23

Reputation: 9019

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  ]]

Upvotes: 1

Related Questions