0

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?

0

2 Answers 2

5

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')
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! I am new to Numpy and tried everything except for this :)
0

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)

1 Comment

Don't recommend np.append. It confuses people. It is just anther way of using concatenate.

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.