0

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]]])
1
  • 1
    Your output is the tuple (my_array.tolist(), second_array.tolist()), but your description is not quite that Commented Nov 6, 2018 at 20:43

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

You are right; I forgot to add the quotation marks when typing the question. I am sorry; my mistake. Your answer is correct though. Thank you!!
Do you want string dtype for the whole array, or a mix of integers and strings? What do you intend to do with these later?
I would prefer a mix of integers and strings
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.