问题描述
我正在尝试将嵌套的 JSON 提取到具有以下结构的数据帧中:
TypeError: Cannot read property 'loadSync' of undefined
at Object.token (C:/Program Files/Heroku/client/node_modules/heroku-cli-util/lib/auth.js:199:9)
at run (C:/Program Files/Heroku/client/node_modules/heroku-cli-util/lib/command.js:156:38)
at Object.run (C:/Program Files/Heroku/client/node_modules/heroku-cli-util/lib/command.js:169:12)
at V5.run (C:/Program Files/Heroku/client/node_modules/@oclif/plugin-legacy/lib/index.js:145:26)
at V5._run (C:/Program Files/Heroku/client/node_modules/@oclif/command/lib/command.js:44:31)
我尝试使用 json_normalize 进行提取,但我被第二级“值”卡住了。我认为这与 normalize 可以使用的级别数有关。有没有另一种方法可以在数据框中获取此结构?
我的代码是:
[
{'id': '1','sort': 'kg','name': 'meal','detail':
[
{'subid': 'A','name': 'Example','values':
[
{'value': '3','time': 0320},{'value': '4','time': 0330},
解决方法
它不像单行那样优雅,但它可以处理多个嵌套级别:
jsondata = [
{'id': '1','sort': 'kg','name': 'meal','detail':
[
{'subid': 'A','name': 'Example','values':
[
{'value': '3','time': 320},{'value': '4','time': 330},{'value': '5','time': 340}
]
}
]
}
]
import pandas as pd
rows = []
for d1 in jsondata:
row = {}
row.update( d1 )
del row['detail']
for d2 in d1['detail']:
row['subid'] = d2['subid']
row['namex'] = d2['name']
for d3 in d2['values']:
row.update(d3)
rows.append( row )
o = pd.DataFrame(rows)
print(o)
,
你可以使用 flatten_json
from flatten_json import flatten
jsondata = [
{'id': '1','time': 340}
]
}
]
}
]
dic_flattened = (flatten(d,'.') for d in jsondata)
df = pd.DataFrame(dic_flattened)
输出
id sort name detail.0.subid detail.0.name detail.0.values.0.value detail.0.values.0.time detail.0.values.1.value detail.0.values.1.time detail.0.values.2.value detail.0.values.2.time
0 1 kg meal A Example 3 320 4 330 5 340