[英]List of dictionaries with multiple values per key as Dataframe
I want to convert a list of dictionaries with multiple values per key to a Dataframe.我想将每个键具有多个值的字典列表转换为 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: Dataframe 应如下所示:
index指数 | Date日期 | value1价值1 | value2价值2 | value3价值3 | value4价值4 |
---|---|---|---|---|---|
0 0 | 2020-12-31 00:00:00 2020-12-31 00:00:00 | 123 123 | 456 456 | 789 789 | 321 321 |
1 1 | 2021-05-06 00:00:00 2021-05-06 00:00:00 | 999 999 | 888 888 | 777 777 | 666 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.我知道使用pd.DataFrame(dictionaries)
可以轻松实现创建带有字典列表的 Dataframe 但我想将值拆分为单独的列。
I appreciate any help.我很感激任何帮助。
pd.DataFrame(d).stack().apply(
pd.Series).add_prefix(
'Value').reset_index().rename(columns={'level_0':'index', 'level_1':'Date'})
Output 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.