将字典列表字典转换为Pandas数据框

时间:2020-07-31 10:06:59

标签: python pandas dataframe

我在下面给出的结构中有一个字典,我需要将其转换为Pandas Dataframe进行一些聚合操作。但是我无法将其转换为给定的输出格式

{
  "raj": [
    {
      "subject": "science",
      "score": "35",
      "create_time": "1595990288421"
    },
    {
      "subject": "maths",
      "score": "40",
      "create_time": "1595980800000"
    }
  ],
  "dev": [
    {
      "subject": "science",
      "score": "23",
      "create_time": "1595990288421"
    }
  ],
  "mag":[
    {
      "subject": "science",
      "score": "41",
      "create_time": "1595990288421"
    },
    {
      "subject": "maths",
      "score": "25",
      "create_time": "1595980800000"
    }
  ]
}

我需要输出数据框为给定格式

name    subject     score   create_time
------------------------------------------
raj     science     35      1595990288421
raj     maths       40      1595980800000
dev     science     23      1595990288421
mag     science     35      1595990288421
mag     maths       25      1595980800000

有人可以帮助我将字典转换为所需的数据帧输出吗?

1 个答案:

答案 0 :(得分:7)

使用列表理解

例如:

AudioBuffer

输出:

df = pd.DataFrame([{'name': k, ** j} for k, v in data.items() for j in v])
print(df)