2

I have 3 arrays of 3x1 in a list, that I want to append to a list of lists, where each list is a single row 1x3 from each array.

for example, convert this:

[array([['0.6913'],
       ['0.7279'],
       ['0.724']], dtype=object), 
array([['0.6943'],
       ['0.7206'],
       ['0.714']], dtype=object), 
array([['0.6456'],
       ['0.7447'],
       ['0.7378']],dtype=object)]

to:

   [[0.6913, 0.6943, 0.6456],
    [0.7279, 0.7206, 0.7447],
    [0.724, 0.714, 0.7378]]

How can I do this? thank you in advance!

1 Answer 1

3

Use numpy.hstack:

import numpy as np

a = [
    np.array([['0.6913'], ['0.7279'], ['0.724']], dtype=object),
    np.array([['0.6943'], ['0.7206'], ['0.714']], dtype=object),
    np.array([['0.6456'], ['0.7447'], ['0.7378']], dtype=object),
]

np.hstack(a)
array([['0.6913', '0.6943', '0.6456'],
       ['0.7279', '0.7206', '0.7447'],
       ['0.724', '0.714', '0.7378']], dtype=object)

To convert to float use .astype

np.hstack(a).astype(float)
array([[0.6913, 0.6943, 0.6456],
       [0.7279, 0.7206, 0.7447],
       [0.724 , 0.714 , 0.7378]])
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! I learned something new today!
You're welcome, glad i could help.

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.