Convert list of lists of lists to 2D np array

Question:

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 4×5 array will be the lists of 3 numbers of the initial list.

I want it to be a (4,5) because I need to use it to populate a dataframe which requires a 2D array as input. Can I somehow "make" the array not to consider the list of 3 elements as yet another list but rather like a 1 "object"?

Any ideas how to do that?

Asked By: Jimakos

||

Answers:

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

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

result1

But as the other comments say, there’s not much you could do with this dataframe. One of the main reasons to store data as a df is to use vectorized methods but that’s not possible with this.

A more sensible approach is to construct a multi index dataframe where each "column" in lst is its own column.

# reshape 3D -> 2D + build df
df = pd.DataFrame(np.reshape(lst, (len(lst), -1)))
# convert the columns to a 5x3 multi-index
df.columns = pd.MultiIndex.from_arrays(np.divmod(df.columns, len(lst[0][0])))
df

result2

Answered By: not a robot
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.