Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

appending lists of list with column values from an array

I have 3 arrays of 3×1 in a list, that I want to append to a list of lists, where each list is a single row 1×3 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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

   [[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!

>Solution :

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]])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading