我有以下json数据
{
"accountId": 999998,
"actionTime": 1528381455013,
"additionalInformation": {
"eS": [
{
"labels": [
"R"
],
"sc": 1.0,
"title": "Company"
},
{
"labels": [
"Slurpee"
],
"sc": 1.0,
"title": "Product"
}
]
},
"apiStatus": "Removed fields because of resyndication policy",
"archived": false,
}
我想展平additionalInformation.eS
的数据。我想要在我的csv中的最终列(通过dataframe.to_csv)是:
label, title, accountId, actionTime
["R"], Comapny, 999998, 1528381455013
["Slurpee"], Product, 999998, 1528381455013
我试过了:
data_frame = json_normalize(data, record_path=["additionalInformation"], meta=[["eS", "label"]], errors='ignore')
data_frame = json_normalize(data, record_path=["additionalInformation", "eS"], meta=["label"], errors='ignore')
我已将Delta function和Stackoverflow answer提交给json_normalize函数
大多数示例都包含字典列表为record_path,我的示例将字典值作为列表。 请帮忙。 感谢
答案 0 :(得分:0)
我确信有一种不那么详细的方法可以做到这一点,但这里有一个选项:
from pandas.io.json import json_normalize
data = {
"accountId": 999998,
"actionTime": 1528381455013,
"additionalInformation": {
"eS": [{
"labels": [
"R"
],
"sc": 1.0,
"title": "Company"
},
{
"labels": [
"Slurpee", "other"
],
"sc": 1.0,
"title": "Product"
}
]
},
"apiStatus": "Removed fields because of resyndication policy"
}
ac = data['accountId']
at = data['actionTime']
r = json_normalize(data['additionalInformation'], record_path = 'eS')
r['accountId'] = ac
r['actionTime'] = at
r = r.drop('sc', axis = 1)
r
给出: