0

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?

1 Answer 1

3

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.