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

Ivo Maljevic ivo.maljevic@gmail....
Fri Jun 6 11:38:44 CDT 2008


The simplest thing is to use c_[ ] operator.

Example:
>>> a=ones([2,2])
>>> a
array([[ 1.,  1.],
       [ 1.,  1.]])
>>> b=array([4,4])
>>> c=array([9,9])
>>> d=c_[a,b,c]
>>> d
array([[ 1.,  1.,  4.,  9.],
       [ 1.,  1.,  4.,  9.]])

Hope this helps,
Ivo


2008/6/6 Dharhas Pothina <Dharhas.Pothina@twdb.state.tx.us>:

> Hi,
>
> 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]])
>
> I've tried hstack but I'm not sure how to use it with 2D arrays.
>
> thanks
>
> - dharhas
> _______________________________________________
> SciPy-user mailing list
> SciPy-user@scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/scipy-user/attachments/20080606/5f215aa3/attachment.html 


More information about the SciPy-user mailing list