【发布时间】:2020-05-10 07:42:33
【问题描述】:
我找不到与我的问题类似的答案,所以我们开始吧:
我有以下形式的字典:
d = {key_1:
{
metric_1: [value_11, value_12],
metric_2: [value_13, value_14],
metric_3: value_15
},
key_2: {
metric_1: [value_21],
metric_2: [value_22],
metric_3: value_23
}
}
如您所见,指标在其列表中包含的项目数量不同。
将其转换为 df 的好方法是什么?如果我使用 from_dict 方法,我最终会得到包含列表的 df 单元格(坏)。
我想要实现的是为列表中的每个值创建一个新行,并将键作为索引:
index | metric_1 | metric_2 | metric_3
———————————————————————————————————————
key_1 | value_11 | value_13 | value_15
key_1 | value_12 | value_14 | value_15
key_2 | value_21 | value_22 | value_23
想法? :)
【问题讨论】:
标签: python pandas dataframe dictionary nested