Fluxy
Fluxy

Reputation: 2978

Hot to create pandas DataFrame from the list of arrays?

I have data structured as follows:

listOfArrays = 
    [array([3, 21, 28]),
     array([13, 14, 2, 29]),
     array([2, 21])]

I need to convert it into a pandas DataFrame with 1 column:

col1
3
21
28
13
14
2
29
2
21

Currently I create pandas DataFrames one by one:

df1 = pd.DataFrame(lisOfLists[0])
df2 = pd.DataFrame(lisOfLists[1])
...

and the I concatenate them using pd.concat.

Is there any shorter approach?

Upvotes: 1

Views: 655

Answers (4)

Nk03
Nk03

Reputation: 14949

You can just load the list into a dataframe and then use melt:

df = pd.DataFrame(listOfArrays).melt()[['value']]

via stack and reset_index:

df = pd.DataFrame(listOfArrays).stack().reset_index()[[0]]

or can also use itertools chain:

from itertools import chain
df = pd.DataFrame({'col1':list(chain(*listOfArrays))})

Upvotes: 0

Franco Morero
Franco Morero

Reputation: 559

A solution using itertool chain.from_iterable:

from numpy import array
import pandas as pd
import itertools

list_of_arrays = [array([3, 21, 28]), array([13, 14, 2, 29]), array([2, 21])]
df = pd.DataFrame(itertools.chain.from_iterable(list_of_arrays), columns=['col1'])
print(df)

output:

   col1
0     3
1    21
2    28
3    13
4    14
5     2
6    29
7     2
8    21

Upvotes: 1

Corralien
Corralien

Reputation: 120559

>>> pd.DataFrame({"col1": np.concatenate(listOfArrays, axis=0)})
   col1
0     3
1    21
2    28
3    13
4    14
5     2
6    29
7     2
8    21

Upvotes: 1

Matteo Zanoni
Matteo Zanoni

Reputation: 4152

You can do it with list comprehensions:

from numpy import array
import pandas as pd

listOfArrays = [array([3, 21, 28]), array([13, 14, 2, 29]), array([2, 21])]

df = pd.concat([pd.Series(array) for array in listOfArrays])

Upvotes: 1

Related Questions