我有一个类似于JSON的数据 -
[
{
"key": "033298fd-f792-4343-b145-852e9cdb680a",
"value": {
"total": 15452,
"history": {
"2019-11-05T23:05:14.53878Z": {
"challenge": "readrules",
"increase": 1,
"total": 1
},
"2019-11-05T23:17:48.849886Z": {
"challenge": "looksthesame",
"increase": 100,
"total": 1601
}
},
...
...
...
}
.... 2nd 'key' ....
}
每个'键'后面跟着一个具有 history
的 'value'。'history' 再次具有一个'键',它是 timestamp
,并具有挑战细节 challenge
、increase
和 total
的值。
我想将其转换为 Pandas 数据帧,看起来类似于 -
key timestamp challenge increase total
033298fd-f792-4343-b145-852e9cdb680a 2019-11-05T23:05:14.53878Z readrules 1 1
033298fd-f792-4343-b145-852e9cdb680a 2019-11-05T23:17:48.84986Z looksthesame 100 101
我试图使用 -
pd.io.json.json_normalize(json)
但这只是将整个 JSON 扁平化为不同的列。
json_normalize
并不能神奇地给你所需的结构。你需要重新构造输入的JSON,使数组中的每个元素都代表一行,例如在你的情况下是timestamp
。 - Vishnudev Krishnadas