nanachan
nanachan

Reputation: 1131

How to join two arrays in a tuple into one array in numpy

I have a tuple with two arrays and I want to make it one array:

The tuple:

(array([['No', 'Yes', 'No', 'No'],
       ['No', 'Yes', 'No', 'Yes'],
       ['No', 'No', 'No', 'Yes']], dtype='<U7'), 
array([['Yes', 'No', 'No', 'Yes']], dtype='<U7'))

I need to make it one array, so that it looks like :

   (array([['No', 'Yes', 'No', 'No'],
           ['No', 'Yes', 'No', 'Yes'],
           ['No', 'No', 'No', 'Yes'],
           ['Yes', 'No', 'No', 'Yes']], dtype='<U7'))

How can I do this?

Upvotes: 0

Views: 1860

Answers (2)

Germ&#225;n Aquino
Germ&#225;n Aquino

Reputation: 126

You can also do this:

t = (array([['No', 'Yes', 'No', 'No'],
       ['No', 'Yes', 'No', 'Yes'],
       ['No', 'No', 'No', 'Yes']], dtype='<U7'), 
array([['Yes', 'No', 'No', 'Yes']], dtype='<U7'))

np.append(t[0], t[1], axis=0)

Upvotes: 0

mgilson
mgilson

Reputation: 310049

Just np.vstack them

np.vstack(tuple_of_array)

example from my terminal:

>>> import numpy as np
>>> array = np.array  # Because I'm lazy and wanted to copy/paste your input ;-)
>>> arrays = (array([['No', 'Yes', 'No', 'No'],
...        ['No', 'Yes', 'No', 'Yes'],
...        ['No', 'No', 'No', 'Yes']], dtype='<U7'), 
... array([['Yes', 'No', 'No', 'Yes']], dtype='<U7'))
>>> np.vstack(arrays)
array([[u'No', u'Yes', u'No', u'No'],
       [u'No', u'Yes', u'No', u'Yes'],
       [u'No', u'No', u'No', u'Yes'],
       [u'Yes', u'No', u'No', u'Yes']], 
      dtype='<U7')

Upvotes: 5

Related Questions