spitfiredd
spitfiredd

Reputation: 3125

Pandas: Create a dataframe from a list of dictionaries where the values are all numpy arrays?

I have list of dictionaries with the values are all np arrays.

It looks something like (there are more columns but this is a good representation)

[
    {'begin_bal': np.arange(100), 'prin': np.arange(100)},
    {'begin_bal': np.arange(50), 'prin': np.arange(50)},
    {'begin_bal': np.arange(360), 'prin': np.arange(260)},
]

I want to create a dataframe that looks like the follow:

begin_bal  prin
1          1
2          2
...        ...
99         99
100        100
1          1
2          2
...        ...
49         49
50         50
1          1
2          2
...        ...
359        359
360        360

The size of each np array is the same for each dictionary.

Upvotes: 2

Views: 57

Answers (2)

BENY
BENY

Reputation: 323236

You can

d1=pd.DataFrame(l)
pd.DataFrame({'begin_bal':np.concatenate(d1['begin_bal'].values),'prin':np.concatenate(d1['prin'].values)})

Upvotes: 1

Denziloe
Denziloe

Reputation: 8131

Use pd.concat.

import numpy as np
import pandas as pd

datas = [
    {'begin_bal': np.arange(100), 'prin': np.arange(100)},
    {'begin_bal': np.arange(50), 'prin': np.arange(50)},
    {'begin_bal': np.arange(360), 'prin': np.arange(360)},
]

pd.concat(pd.DataFrame(data) for data in datas)

Upvotes: 3

Related Questions