How to save list of arrays in dataframe

Viewed 176

Can someone help me if there is solution how to save list of arrays in Python in dataframe but this input(list of arrays) changes in cycle so that I need to every array store as new dataframe. Here is the example:

If my input seems look like this:

[array([[ 1,  2,  3],
       [1, 2, 3]]), array([[3, 4, 5],
       [ 3,  4,  5]])]

I need output as dataframes like this:

1. column 2. column 3. column
1 2 3
1 2 3
1. column 2. column 3. column
3 4 5
3 4 5

but this is only for first iteration in cycle so when the second iteration start I get new data e.g.

[array([[ 6,  7,  8],
       [6, 7, 8]]), array([[9, 10, 11],
       [ 9,  10,  11]])]

so I need to update my dataframe first array append to first dataframe and the second array to the second dataframe so the updated table should look like this:

1. column 2. column 3. column
1 2 3
1 2 3
6 7 8
6 7 8
1. column 2. column 3. column
3 4 5
3 4 5
9 10 11
9 10 11

Is there any way how to make this ?

1 Answers

simply make an data frame out of the sub array, then append it to your main dataframe:

import pandas as pd
data = [array([[ 1,  2,  3],
       [1, 2, 3]]), array([[3, 4, 5],
       [ 3,  4,  5]])]

df = pd.DataFrame({"A": [], "B": [], "C": []})

for arr in data:
    subdf = pd.DataFrame(arr, ["A","B","C"])
    df = df.append(subdf)

this returns:

A B C
1 2 3
1 2 3
3 4 5
3 4 5
Related