pandas.json_normalize #

熊猫。json_normalize ( data , record_path = None , meta = None , meta_prefix = None , record_prefix = None , error = 'raise' , sep = '.' , max_level = None ) [来源] #

将半结构化 JSON 数据规范化为平面表。

参数
数据字典或字典列表

未序列化的 JSON 对象。

record_path str 或 str 列表,默认 None

每个对象中记录列表的路径。如果未传递,数据将被假定为记录数组。

路径元列表(str 或 str 列表),默认 None

用作结果表中每条记录的元数据的字段。

meta_prefix str,默认无

如果为 True,则在记录前添加点 (?) 路径,例如,如果元为 ['foo', 'bar'],则为 foo.bar.field。

record_prefix str,默认无

如果为 True,则在记录前添加点 (?) 路径,例如,如果记录路径为 ['foo', 'bar'],则为 foo.bar.field。

错误{'raise', 'ignore'}, 默认 'raise'

配置错误处理。

  • 'ignore' :如果元中列出的键并不总是存在,将忽略 KeyError。

  • 'raise' :如果元中列出的键并不总是存在,将引发 KeyError。

sep字符串,默认 '.'

嵌套记录将生成以 sep 分隔的名称。例如,对于 sep='.', {'foo': {'bar': 0}} -> foo.bar。

max_level int,默认无

要标准化的最大级别数(字典深度)。如果没有,则标准化所有级别。

返回
数据帧
将半结构化 JSON 数据规范化为平面表。

例子

>>> data = [
...     {"id": 1, "name": {"first": "Coleen", "last": "Volk"}},
...     {"name": {"given": "Mark", "family": "Regner"}},
...     {"id": 2, "name": "Faye Raker"},
... ]
>>> pd.json_normalize(data)
    id name.first name.last name.given name.family        name
0  1.0     Coleen      Volk        NaN         NaN         NaN
1  NaN        NaN       NaN       Mark      Regner         NaN
2  2.0        NaN       NaN        NaN         NaN  Faye Raker
>>> data = [
...     {
...         "id": 1,
...         "name": "Cole Volk",
...         "fitness": {"height": 130, "weight": 60},
...     },
...     {"name": "Mark Reg", "fitness": {"height": 130, "weight": 60}},
...     {
...         "id": 2,
...         "name": "Faye Raker",
...         "fitness": {"height": 130, "weight": 60},
...     },
... ]
>>> pd.json_normalize(data, max_level=0)
    id        name                        fitness
0  1.0   Cole Volk  {'height': 130, 'weight': 60}
1  NaN    Mark Reg  {'height': 130, 'weight': 60}
2  2.0  Faye Raker  {'height': 130, 'weight': 60}

将嵌套数据标准化至级别 1。

>>> data = [
...     {
...         "id": 1,
...         "name": "Cole Volk",
...         "fitness": {"height": 130, "weight": 60},
...     },
...     {"name": "Mark Reg", "fitness": {"height": 130, "weight": 60}},
...     {
...         "id": 2,
...         "name": "Faye Raker",
...         "fitness": {"height": 130, "weight": 60},
...     },
... ]
>>> pd.json_normalize(data, max_level=1)
    id        name  fitness.height  fitness.weight
0  1.0   Cole Volk             130              60
1  NaN    Mark Reg             130              60
2  2.0  Faye Raker             130              60
>>> data = [
...     {
...         "state": "Florida",
...         "shortname": "FL",
...         "info": {"governor": "Rick Scott"},
...         "counties": [
...             {"name": "Dade", "population": 12345},
...             {"name": "Broward", "population": 40000},
...             {"name": "Palm Beach", "population": 60000},
...         ],
...     },
...     {
...         "state": "Ohio",
...         "shortname": "OH",
...         "info": {"governor": "John Kasich"},
...         "counties": [
...             {"name": "Summit", "population": 1234},
...             {"name": "Cuyahoga", "population": 1337},
...         ],
...     },
... ]
>>> result = pd.json_normalize(
...     data, "counties", ["state", "shortname", ["info", "governor"]]
... )
>>> result
         name  population    state shortname info.governor
0        Dade       12345   Florida    FL    Rick Scott
1     Broward       40000   Florida    FL    Rick Scott
2  Palm Beach       60000   Florida    FL    Rick Scott
3      Summit        1234   Ohio       OH    John Kasich
4    Cuyahoga        1337   Ohio       OH    John Kasich
>>> data = {"A": [1, 2]}
>>> pd.json_normalize(data, "A", record_prefix="Prefix.")
    Prefix.0
0          1
1          2

返回标准化数据,其中列以给定字符串为前缀。