Home > front end >  How to form a DataFrame from a list of dictionaries with list of values?
How to form a DataFrame from a list of dictionaries with list of values?

Time:10-19

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!

CodePudding user response:

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
  • Related