Reputation: 317
I have a list of list of dictionary with values in list that I would like to be transformed into a dataframe as shown below:
data_list = [
{'Width':[20,25,30,40],'Length':[21,22,23,24],'Ratio':[1.05,0.88,0.767,0.6]},
{'Width':[10,15,24,35],'Length':[20,25,30,40],'Ratio':[2,1.67,1.25,1.14]}
]
And I try to use pd.DataFrame.from_dict(data_list)
, however, the result is undesired as shown below:
| | Width | Length | Ratio |
|---| -------------- | ------------ | --------------------- |
| 0 | [20,25,30,40] | [21,22,23,24] | [1.05,0.88,0.767,0.6] |
| 1 | [10,15,24,35] | [20,25,30,40] | [2,1.67,1.25,1.14] |
What I expect is as below:
| | Width | Length | Ratio |
|---| ----- | ------ | ----- |
| 0 | 20 | 21 | 1.05 |
| 1 | 25 | 22 | 0.88 |
......
| 6 | 24 | 30 | 1.25 |
| 7 | 35 | 40 | 1.14 |
Any help would be great! Thanks!
Upvotes: 3
Views: 87
Reputation: 863741
Use solution from comments:
df = pd.DataFrame(data_list).apply(pd.Series.explode).reset_index(drop=True)
print (df)
Width Length Ratio
0 20 21 1.05
1 25 22 0.88
2 30 23 0.767
3 40 24 0.6
4 10 20 2
5 15 25 1.67
6 24 30 1.25
7 35 40 1.14
Another idea is use defaultdict
with extend
, last pass to DataFrame
constructor for improve performance:
from collections import defaultdict
d = defaultdict(list)
for di in data_list:
for k, v in di.items():
d[k].extend(v)
df = pd.DataFrame(d)
print (df)
Width Length Ratio
0 20 21 1.050
1 25 22 0.880
2 30 23 0.767
3 40 24 0.600
4 10 20 2.000
5 15 25 1.670
6 24 30 1.250
7 35 40 1.140
Upvotes: 1
Reputation: 28729
Another option would be to concatenate a list of DataFrames:
pd.concat([pd.DataFrame(ent) for ent in data_list], ignore_index = True)
Width Length Ratio
0 20 21 1.050
1 25 22 0.880
2 30 23 0.767
3 40 24 0.600
4 10 20 2.000
5 15 25 1.670
6 24 30 1.250
7 35 40 1.140
Upvotes: 0
Reputation: 110
I think this can do your need if you don't need index.
import pandas as pd
data_list = [
{'Width':[20,25,30,40],'Length':[21,22,23,24],'Ratio':[1.05,0.88,0.767,0.6]},
{'Width':[10,15,24,35],'Length':[20,25,30,40],'Ratio':[2,1.67,1.25,1.14]}
]
for data in data_list:
df = df.append(pd.DataFrame(data))
Upvotes: 0