astruso99
astruso99

Reputation: 1

From list of dataframe to array of array in python

as the title says, I have this list called "list", containing multiple Dataframes (shape 120 x 120) with some numeric data, added from a previous list.

...
df_sum = list_dataframe[0]
    for i in range (1, len(list_dataframe)):
        df_sum = df_sum.add(list_dataframe[i])

    list.append(df_sum)

Let's say that "list" contains 800 dataframes, so every index of this list contains a dataframe. I want to:

  1. create an array with the same length of "list"
  2. take every dataframe in "list", one by one, convert it into a Numpy array (120 x 120, so a matrix)
  3. add every Numpy array (120 x 120) into the array created (800).

So i want to obtain an array (with a length of 800, same of list), where every index contains one of the 800 Numpy array (matrix).

I have already used .to_numpy() function applied to the list with a for loop,

for i in range(len(list)):
    list[i] = list[i].to_numpy()

but it generates a strange structure, like an array of array of array where the second one contains only one element, that is the dataframe converted into an array:

>>> list 

>>>[array([[0., 0., 0., ..., 0., 0., 0.],
        [0., 0., 0., ..., 0., 0., 0.],
        [0., 0., 0., ..., 0., 0., 0.],
        ...,
        [0., 0., 0., ..., 0., 0., 0.],
        [0., 0., 0., ..., 0., 0., 0.],
        [0., 0., 0., ..., 0., 0., 0.]]),
 array([[0., 0., 0., ..., 0., 0., 0.],
        [0., 0., 0., ..., 0., 0., 0.],
        [0., 0., 0., ..., 0., 0., 0.],
        ...,

How can I do that?

Upvotes: 0

Views: 66

Answers (1)

Chrysophylaxs
Chrysophylaxs

Reputation: 6583

You're on the right track. If you call np.array on your resulting list, it will create one large array that has shape (800, 120, 120). An example using a list comprehension instead of a for-loop:

import numpy as np
import pandas as pd

my_list = [pd.DataFrame(np.random.randint(10, size=(120, 120))) for _ in range(800)]

out = np.array([df.to_numpy() for df in my_list])
>>> out.shape
(800, 120, 120)

Upvotes: 3

Related Questions