python - 在 Python 中取消嵌套/规范化 JSON

标签 python json pandas

我试图在这里取消嵌套国会数据:https://theunitedstates.io/congress-legislators/legislators-historical.json

示例结构:

    {
    "id": {
      "bioguide": "B000226",
      "govtrack": 401222,
      "icpsr": 507,
      "wikipedia": "Richard Bassett (politician)",
      "wikidata": "Q518823",
      "google_entity_id": "kg:/m/02pz46"
    },
    "name": {
      "first": "Richard",
      "last": "Bassett"
    },
    "bio": {
      "birthday": "1745-04-02",
      "gender": "M"
    },
    "terms": [
      {
        "type": "sen",
        "start": "1789-03-04",
        "end": "1793-03-03",
        "state": "DE",
        "class": 2,
        "party": "Anti-Administration"
      }
    ]
  }

如果我只使用 json_normalize(data),“术语”不会解除嵌套。

如果我尝试专门取消嵌套术语,例如 json_normalize(data, 'terms', 'name'),那么我包含的其他内容(此处为名称)将保持为 dict 格式 {u'last': u'Bassett', u'first': u'Richard'} 作为行条目。

完整的当前代码,如果您想运行它:

import json
import urllib
import pandas as pd
from pandas.io.json import json_normalize

# load data
url = "https://theunitedstates.io/congress-legislators/legislators-historical.json"
json_url = urllib.urlopen(url)
data = json.loads(json_url.read())

# parse
congress_names = json_normalize(data, record_path='terms',meta='name')

最佳答案

我认为下面的代码应该可以工作。可能有更好的标准化方法,但我不知道。

import requests
import pandas as pd
import re
import json
from pandas.io.json import json_normalize

url = ' https://theunitedstates.io/congress-legislators/legislators-historical.json'
resp = requests.get(url)
raw_dict = json.loads(resp.text)

df = pd.DataFrame()
for i in range(len(raw_dict)):    
     df1 = json_normalize(raw_dict[i], record_path = ['terms'], meta = ['name'])
     df1 = pd.concat([df1, df1['name'].apply(pd.Series)], axis=1)
     df = pd.concat([df,df1], axis=0, ignore_index =True, sort=True)

关于python - 在 Python 中取消嵌套/规范化 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59673313/

相关文章:

python - 我有一个带有列表的 Pandas 专栏。对包含同一列中至少一个公共(public)元素的行进行分组

javascript - 使用 jQuery.parseJSON() 解析 JSON 字符串

javascript - 对于每个LOOP通过JSON.Stringify图像的EXIF数据

java - IOS应用程序从MySql数据库获取数据

python - 迭代 pandas 数据框中的行并匹配列表字典中的值以创建新列

python - 寻找在巨大的 Pandas Dataframe 中对一行进行切片的最快方法

python - 调试(过早?)OOM-killer 输出

python - 来自 NetworkX 度值的直方图 - Python 2 与 Python 3

python - 递归访问嵌套字典的路径和值

python - 在具有相似但不精确值的列上连接数据框