AdeEla
AdeEla

Reputation: 289

How do I add a list to an ArrayList in Python Numpy

If I have two arrays:

import numpy as np
my_array = np.array([[1,1,"food",5],
                    [[2,1,"food",5],
                    [2,2,"clothes",10]]])
second_array = np.array ([[3,5,"water",3],
                         [3,2,"tea", 8], 
                         [3,4,"pop", 5]])

and I want to add the second_array into my_array. Does anyone have any idea how could I do that? I have tried np.append but it removes all the lists within the array only storing the value but I want my output to look like:

new_array = ([[1,1,"food",5],
             [[2,1,"food",5],
              [2,2,"clothes",10]]
             [[3,5,"water",3],
              [3,2,"tea", 8], 
              [3,4,"pop", 5]]])

Upvotes: 0

Views: 992

Answers (2)

hpaulj
hpaulj

Reputation: 231385

I get an error trying to run your sample:

In [393]: my_array = np.array([[1,1,food,5],
     ...:                     [[2,1,food,5],
     ...:                     [2,2,clothes,10]]])
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-393-0a4854c57a22> in <module>()
----> 1 my_array = np.array([[1,1,food,5],
      2                     [[2,1,food,5],
      3                     [2,2,clothes,10]]])
      4 second_array = np.array ([[3,5,water,3],
      5                          [3,2,tea, 8],

NameError: name 'food' is not defined

changing the names to strings:

In [394]: my_array = np.array([[1,1,'food',5],
     ...:                     [[2,1,'food',5],
     ...:                     [2,2,'clothes',10]]])
     ...: second_array = np.array ([[3,5,'water',3],
     ...:                          [3,2,'tea', 8], 
     ...:                          [3,4,'pop', 5]])
     ...:                          
In [395]: my_array
Out[395]: 
array([list([1, 1, 'food', 5]),
       list([[2, 1, 'food', 5], [2, 2, 'clothes', 10]])], dtype=object)
In [396]: second_array
Out[396]: 
array([['3', '5', 'water', '3'],
       ['3', '2', 'tea', '8'],
       ['3', '4', 'pop', '5']], dtype='<U21')

Those are two vary different kinds of arrays. It doesn't make sense to try to join them in any way.

If I clean up the brackets on the first:

In [397]: my_array = np.array([[1,1,'food',5],
     ...:                       [2,1,'food',5],
     ...:                       [2,2,'clothes',10]])
In [398]: my_array
Out[398]: 
array([['1', '1', 'food', '5'],
       ['2', '1', 'food', '5'],
       ['2', '2', 'clothes', '10']], dtype='<U21')

Now I have 2 arrays with the same dtype and shape, which can be joined in various ways:

In [399]: np.stack((my_array, second_array))
Out[399]: 
array([[['1', '1', 'food', '5'],
        ['2', '1', 'food', '5'],
        ['2', '2', 'clothes', '10']],

       [['3', '5', 'water', '3'],
        ['3', '2', 'tea', '8'],
        ['3', '4', 'pop', '5']]], dtype='<U21')
In [400]: np.vstack((my_array, second_array))
Out[400]: 
array([['1', '1', 'food', '5'],
       ['2', '1', 'food', '5'],
       ['2', '2', 'clothes', '10'],
       ['3', '5', 'water', '3'],
       ['3', '2', 'tea', '8'],
       ['3', '4', 'pop', '5']], dtype='<U21')

We could specify a object dtype when creating the 2 array.

Upvotes: 1

mad_
mad_

Reputation: 8273

use vstack

import numpy as np
my_array = np.array([[1,1,'food',5],
                    [2,1,'food',5],
                    [2,2,'clothes',10]])
second_array = np.array ([[3,5,'water',3],
                         [3,2,'tea', 8], 
                         [3,4,'pop', 5]])

np.vstack([my_array,second_array])

Upvotes: 1

Related Questions