Home > database >  Convert list of lists of lists to 2D np array
Convert list of lists of lists to 2D np array

Time:07-20

I have the following list of lists of lists (4 x 5 x 3):

[
[[0.96, 0.52, 1.0], [1.0, 0.0, 1.0], [0.9166666666666666, 0.0, 1.0], [0.2056, 0.2056, 0.2062], [0.4492, 0.4492, 0.4492]], 
[[0.96, 0.52, 1.0], [1.0, 0.0, 1.0], [0.9166666666666666, 0.0, 1.0], [0.207, 0.2094, 0.2112], [0.4492, 0.4492, 0.4492]], 
[[0.98, 0.96, 0.98], [1.0, 1.0, 1.0], [0.96, 0.92, 0.96], [0.2067, 0.2127, 0.2139], [0.4492, 0.4492, 0.4492]], 
[[0.98, 0.9, 0.98], [1.0, 1.0, 1.0], [0.96, 0.8, 0.96], [0.2075, 0.2156, 0.2172], [0.4492, 0.4492, 0.4492]]]

It's length is 4. Each of the 4 elements is a list of 5 elements and each of those 5 elements is a list of 3 numbers. I want to convert this list to an np array of size (4,5), in other words each element of the new 2D 4x5 array will be the lists of 3 numbers of the initial list. Any ideas how to do that?

CodePudding user response:

Pandas df constructor is really flexible. You can cast any list to a dataframe.

df = pd.DataFrame(lst)
df.shape
# (4, 5)
df

result

But as the other comments say, there's not much you could do with this df.

  • Related