Reputation: 83
I want to convert a list of dictionaries with multiple values per key to a Dataframe.
d = [
{'2020-12-31 00:00:00': [123, 456, 789, 321]},
{'2021-05-06 00:00:00': [999, 888, 777, 666]}
]
The Dataframe should look like this:
index | Date | value1 | value2 | value3 | value4 |
---|---|---|---|---|---|
0 | 2020-12-31 00:00:00 | 123 | 456 | 789 | 321 |
1 | 2021-05-06 00:00:00 | 999 | 888 | 777 | 666 |
I know that creating a Dataframe with a list of dictionaries is easily achievable with pd.DataFrame(dictionaries)
but i would like to split the values into separate columns.
I appreciate any help.
Upvotes: 2
Views: 764
Reputation: 5918
pd.DataFrame(d).stack().apply(
pd.Series).add_prefix(
'Value').reset_index().rename(columns={'level_0':'index', 'level_1':'Date'})
Output
index Date Value0 Value1 Value2 Value3
0 0 2020-12-31 00:00:00 123 456 789 321
1 1 2021-05-06 00:00:00 999 888 777 666
Upvotes: 2