Raghavendra MG
Raghavendra MG

Reputation: 89

Adding a column to a 2D list

I have a 2D list of arrays like

  array( [ 988,  389],
         [ 986,  389],
         [ 985,  388],
         [ 977,  388],
         [ 976,  387]], dtype=int32)

and another list

 array( [ 149.68299837],
        [ 149.25481567],
        [ 150.029997  ],
        [ 148.63714206],
        [ 149.48244044]])

I tried to concatenate these two lists using

  trail = list(map(list,zip(two_d_array,concat)))
  trail = np.vstack(trail)

This gives me

  array([array([988, 389], dtype=int32), array([ 149.68299837])],
        [array([986, 389], dtype=int32), array([ 149.25481567])],
        [array([985, 388], dtype=int32), array([ 150.029997])],
        [array([977, 388], dtype=int32), array([ 148.63714206])],
        [array([976, 387], dtype=int32), array([ 149.48244044])]], dtype=object)

How do I remove all the array and dtype and just display the numbers like

     [ 988,  389,149.68299837],
     [ 986,  389,149.25481567],
     [ 985,  388, 150.029997],
     [ 977,  388,148.63714206],
     [ 976,  387,149.48244044]

Upvotes: 2

Views: 3448

Answers (2)

NaN
NaN

Reputation: 2312

I like np.c_ and np.column_stack ( @Divakar suggestion ) since I care little about timing but I am more interested in how it visually 'looks' for understanding purposes...

>>> a = np.arange(10).reshape(5, 2)
>>> b = np.arange(10,15)
>>> c = np.c_[a,b]
>>> a
array([[0, 1],
       [2, 3],
       [4, 5],
       [6, 7],
       [8, 9]])
>>> b
array([10, 11, 12, 13, 14])
>>> c
array([[ 0,  1, 10],
       [ 2,  3, 11],
       [ 4,  5, 12],
       [ 6,  7, 13],
       [ 8,  9, 14]])
>>> np.column_stack((a,b))
array([[ 0,  1, 10],
       [ 2,  3, 11],
       [ 4,  5, 12],
       [ 6,  7, 13],
       [ 8,  9, 14]])

array a and b are obvious. I just have to remember to do np.c_ square brackets (np.c_[ stack these ] and of course, stack by columns makes sense to me as well.

Upvotes: 5

hpaulj
hpaulj

Reputation: 231385

To do a copy-n-paste of your two arrays I had to a [ at the start The result is 2 2d arrays

In [165]: twod.shape
Out[165]: (5, 2)
In [166]: oned.shape
Out[166]: (5, 1)

A simple concatenate works

In [164]: np.concatenate((twod, oned),axis=1)
Out[164]: 
array([[ 988.        ,  389.        ,  149.68299837],
       [ 986.        ,  389.        ,  149.25481567],
       [ 985.        ,  388.        ,  150.029997  ],
       [ 977.        ,  388.        ,  148.63714206],
       [ 976.        ,  387.        ,  149.48244044]])

Notice that everything is float now.

Your list map calc produces a list like:

[[array([988, 389]), array([ 149.68299837])],
 [array([986, 389]), array([ 149.25481567])],
 [array([985, 388]), array([ 150.029997])],
 ....

Sublists containing 2 arrays of different length. Substituting hstack for list would have produced a list that could be vstacked

In [173]: temp = list(map(np.hstack, zip(twod, oned.ravel())))
In [174]: temp
Out[174]: 
[array([ 988.        ,  389.        ,  149.68299837]),
 array([ 986.        ,  389.        ,  149.25481567]),
 array([ 985.      ,  388.      ,  150.029997]),
....

Upvotes: 0

Related Questions