pandas中DataFrame转换dic类型数据出错,遇到了以下问题,求问该如何解决?
在做一个爬虫的时候,爬来的数据存储为字典嵌套格式,然后使用pandas中DataFrame进行转换时输出会是这样:
但我所期望的结果应该是以date为主键,即将以上结果转置的样子
所以我试着制定主键,得到如下结果:
代码如下:
for i in data_all["chinaDayList"]:
ds = "2020." + i["date"]
tup = time.strptime(ds, "%Y.%m.%d")
ds = time.strftime("%Y-%m-%d", tup)
confirm = i["confirm"]
suspect = i["suspect"]
heal = i["heal"]
dead = i["dead"]
history[ds] = {"confirm": confirm, "suspect": suspect, "heal": heal, "dead": dead}
for i in data_all["chinaDayAddList"]:
ds = "2020." + i["date"]
tup = time.strptime(ds, "%Y.%m.%d")
ds = time.strftime("%Y-%m-%d", tup)
confirm = i["confirm"]
suspect = i["suspect"]
heal = i["heal"]
dead = i["dead"]
history[ds].update({"confirm_add": confirm, "suspect_add": suspect, "heal_add": heal, "dead_add": dead})
print (history)如下: