[SciPy-user] Adding new columns to 2D array & populating them from another array.

Alan G Isaac aisaac at american.edu
Fri Jun 6 12:46:36 EDT 2008


On Fri, 06 Jun 2008, Dharhas Pothina apparently wrote:
> I have an array 'a'

> a = 
> array([[2006,    1,    3,    9,   40], 
>        [2006,    1,    3,   10,    9],
>        [2006,    1,    3,   10,   40],
>        ...,
>        [2008,    3,   20,   10,   27],
>        [2008,    3,   20,   10,   51],
>        [2008,    3,   20,   12,    2]])

> where a.shape = (420, 5)

> I have other arrays b,c etc of the shape (420,) (say b contains all 99's and c contains all 50's)

> how to I add the new arrays to 'a' to form an array 

>   array([[2006,    1,    3,    9,   40 , 99, 50],
>        [2006,    1,    3,   10,    9, 99 , 50],
>        [2006,    1,    3,   10,   40, 99, 50],
>        ...,
>        [2008,    3,   20,   10,   27, 99, 50 ],
>        [2008,    3,   20,   10,   51, 99, 50],
>        [2008,    3,   20,   12,    2, 99, 50]])

>>> import numpy as np
>>> x = np.ones((10,3))
>>> y = np.ones((10,))*2
>>> z = np.ones((10,))*3
>>> np.hstack([x,y[:,None],z[:,None]])
array([[ 1.,  1.,  1.,  2.,  3.],
       [ 1.,  1.,  1.,  2.,  3.],
       [ 1.,  1.,  1.,  2.,  3.],
       [ 1.,  1.,  1.,  2.,  3.],
       [ 1.,  1.,  1.,  2.,  3.],
       [ 1.,  1.,  1.,  2.,  3.],
       [ 1.,  1.,  1.,  2.,  3.],
       [ 1.,  1.,  1.,  2.,  3.],
       [ 1.,  1.,  1.,  2.,  3.],
       [ 1.,  1.,  1.,  2.,  3.]])
>>>

hth,
Alan Isaac




















































More information about the SciPy-User mailing list