将类似dict的数据导入到pandas中

时间:2017-04-24 13:39:37

标签: python pandas dictionary

我有许多以类似dict格式编写的数据文件:

{"score": [0.9995803236961365, 0.00041968212462961674], "key": "Am2mVTMbhd0y", "label": "0"}
{"score": [0.9997120499610901, 0.00028794570243917406], "key": "AmG8StB8hM2k", "label": "0"}
{"score": [0.8841496109962463, 0.11585044860839844], "key": "Alt137zv2nY6", "label": "0"}
{"score": [0.9999467134475708, 5.334055458661169e-05], "key": "AmGdF7cY4X22", "label": "0"}

我想要做的是将它们导入到pandas中,将列作为“key”,“label”和“score” - 并且必须在单独的列中包含两个数值。我尝试将文件导入为dict,但我得到了:

ValueError: too many values to unpack

有关如何解决这个问题的任何建议吗?

2 个答案:

答案 0 :(得分:0)

import pandas as pd

#add your data in a list
data = [{"score": [0.9995803236961365, 0.00041968212462961674], "key": "Am2mVTMbhd0y", "label": "0"},
{"score": [0.9997120499610901, 0.00028794570243917406], "key": "AmG8StB8hM2k", "label": "0"},
{"score": [0.8841496109962463, 0.11585044860839844], "key": "Alt137zv2nY6", "label": "0"},
{"score": [0.9999467134475708, 5.334055458661169e-05], "key": "AmGdF7cY4X22", "label": "0"}]
#create dataframe
df = pd.DataFrame(data)

答案 1 :(得分:0)

我认为您需要read_json中的参数image_mat

lines=True

要将df = pd.read_json('file.json', lines=True) print (df) key label score 0 Am2mVTMbhd0y 0 [0.999580323696136, 0.00041968212462900004] 1 AmG8StB8hM2k 0 [0.9997120499610901, 0.00028794570243900004] 2 Alt137zv2nY6 0 [0.8841496109962461, 0.11585044860839801] 3 AmGdF7cY4X22 0 [0.99994671344757, 5.3340554586611695e-05] print (type(df['score'].iat[0])) <class 'list'> 转换为列,请将lists构造函数与concat一起使用:

DataFrame