ste
ste

Reputation: 458

Reformat table in Python

I have a table in a Python script with numpy in the following shape:

[array([[a1, b1, c1], ..., [x1, y1, z1]]), 
 array([a2, b2, c2, ..., x2, y2, z2])
]

I would like to reshape it to a format like this:

(array([[a2],
        [b2],
         .
         .
         .
        [z2]],
        dtype = ...),
 array([[a1],
        [b1],
         .
         .
         .
        [z1]])
)

To be honest, I'm also quite confused about the different parentheses. array1, array2] is a list of arrays, right? What is (array1, array2), then?

Upvotes: 1

Views: 89

Answers (2)

kiviak
kiviak

Reputation: 1103

 #[array1,array2] is a python list of two numpy tables(narray)
 #(array1,array2) is a python tuple of two numpy tables(narray)
 tuple([array.reshape((-1,1)) for array in your_list.reverse()])

Upvotes: 1

Nico Schlömer
Nico Schlömer

Reputation: 58901

Round brackets (1, 2) are tuples, square brackets [1, 2] are lists. To convert your data structure, use expand_dims and flatten.

import numpy as np

a = [
    np.array([[1, 2, 3], [4, 5, 6]]),
    np.array([10, 11, 12, 13, 14])
    ]
print(a)

b = (
    np.expand_dims(a[1], axis=1),
    np.expand_dims(a[0].flatten(), axis=1)
    )

print(b)

Upvotes: 1

Related Questions