我有一个简单的JSON数据,例如:
[{
"load": 1,
"results": {
"key": "A",
"timing": 1.1
}
}, {
"load": 2,
"results": {
"key": "B",
"timing": 2.2
}
}]
在尝试将其加载到熊猫时:
pd.read_json('res.json')
但是key
并没有嵌套value
,而是嵌套了它们。
如何将它们标准化?
答案 0 :(得分:0)
data = [{
"load": 1,
"results": {
"key": "A",
"timing": 1.1
}
}, {
"load": 2,
"results": {
"key": "B",
"timing": 2.2
}
}]
from pandas.io.json import json_normalize
df = json_normalize(data)
print (df)
load results.key results.timing
0 1 A 1.1
1 2 B 2.2
如果需要文件中的数据:
from pandas.io.json import json_normalize
import json
with open('sample.json') as f:
data = json.load(f)
df = json_normalize(data)