polars.json_normalize#
- polars.json_normalize(
- data: dict[Any, Any] | Sequence[dict[Any, Any] | Any],
- *,
- separator: str = '.',
- max_level: int | None = None,
- schema: Schema | None = None,
- strict: bool = True,
- infer_schema_length: int | None = 100,
将半结构化的反序列化JSON数据规范化为一个扁平表。
不会被解嵌套/规范化的字典对象被编码为json字符串数据。与pandas的对应函数不同,此函数不会在任何级别将字典编码为对象。
警告
此功能被视为不稳定。它可能会在任何时候更改,而不被视为破坏性更改。
- Parameters:
- data
反序列化的JSON对象。
- separator
嵌套记录将生成由分隔符分隔的名称。例如, 对于
separator=".", {"foo": {"bar": 0}}-> foo.bar。- max_level
最大层级数(字典的深度)以进行标准化。 如果为None,则标准化所有层级。
- schema
当规范化数据传递给
DataFrame构造函数时,覆盖Schema。- strict
Polars 在构建 DataFrame 时是否应该严格。
- infer_schema_length
用于确定模式的行数。
示例
>>> 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}, ... }, ... ] >>> pl.json_normalize(data, max_level=1) shape: (3, 4) ┌──────┬────────────┬────────────────┬────────────────┐ │ id ┆ name ┆ fitness.height ┆ fitness.weight │ │ --- ┆ --- ┆ --- ┆ --- │ │ i64 ┆ str ┆ i64 ┆ i64 │ ╞══════╪════════════╪════════════════╪════════════════╡ │ 1 ┆ Cole Volk ┆ 130 ┆ 60 │ │ null ┆ Mark Reg ┆ 130 ┆ 60 │ │ 2 ┆ Faye Raker ┆ 130 ┆ 60 │ └──────┴────────────┴────────────────┴────────────────┘ >>> pl.json_normalize(data, max_level=0) shape: (3, 3) ┌──────┬────────────┬───────────────────────────────┐ │ id ┆ name ┆ fitness │ │ --- ┆ --- ┆ --- │ │ i64 ┆ str ┆ str │ ╞══════╪════════════╪═══════════════════════════════╡ │ 1 ┆ Cole Volk ┆ {"height": 130, "weight": 60} │ │ null ┆ Mark Reg ┆ {"height": 130, "weight": 60} │ │ 2 ┆ Faye Raker ┆ {"height": 130, "weight": 60} │ └──────┴────────────┴───────────────────────────────┘