Skip to main content

Pandas-like toolkit for nested JSON/NDJSON: flatten, select, explode, reconstruct

Project description

nestedframe

面向嵌套 JSON/NDJSON 的 Pandas 风格数据工具。目标是让处理半结构化的事件日志、API 响应、埋点、NDJSON 文件像用 Pandas 一样顺畅:一行载入、路径选择、受控扁平化、数组爆炸、再构造成嵌套结构。

  • 痛点:

    • json_normalize 很快失控,数组字段处理复杂,丢层级关系。
    • 需要在“保留原始嵌套结构”与“产生可分析的扁平表”之间来回切换。
    • 选择嵌套字段、匹配多层路径、对数组进行受控 explode 并保持父子关联很麻烦。
  • 解决:

    • NestedFrame 提供 from_recordsselect('user.*')explode('items')to_nested(group_by='_root_id') 等 API。
    • 列名采用点路径表示,如 user.idmeta.country,数组 explode 时自动展开子字段为 items.iditems.qty

安装

pip install -e .

快速上手

from nestedframe import NestedFrame

records = [
    {"user": {"id": 1, "name": "A"}, "items": [{"id": "i1", "qty": 2}, {"id": "i2", "qty": 1}], "meta": {"country": "CN"}},
    {"user": {"id": 2, "name": "B"}, "items": [{"id": "i3", "qty": 5}], "meta": {"country": "US"}}
]

nf = NestedFrame.from_records(records)
df = nf.to_pandas()

nf2 = nf.explode("items")
exploded = nf2.to_pandas()

nested = nf2.to_nested(group_by="_root_id")

主要 API

  • NestedFrame.from_records(records) 载入嵌套数据
  • select(patterns) 支持 fnmatch 风格的路径筛选,如 user.*meta.country
  • explode(path) 对数组字段受控爆炸,并将子字段扁平为点路径
  • to_pandas() 获取扁平化 DataFrame
  • to_nested(group_by=None) 将表再构造成嵌套记录;当使用过 explode 时建议按 _root_id 分组

适用场景

  • 大量 NDJSON 事件日志分析
  • API 响应批量整形、聚合和统计
  • 埋点数据质量分析与快速探索

许可

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nestedframe-0.1.0.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nestedframe-0.1.0-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file nestedframe-0.1.0.tar.gz.

File metadata

  • Download URL: nestedframe-0.1.0.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for nestedframe-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f3bbd54af19604bc8ece35d7a8db23531c111a00c8f71fddcb278d51b8bed5c6
MD5 7e478a5a10c953b6c3bdaa439b8f50e5
BLAKE2b-256 674a9b3110515c994e3f8e6978b26f309439e9aee2d409df32deb35c6ac8d0ca

See more details on using hashes here.

File details

Details for the file nestedframe-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: nestedframe-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for nestedframe-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c9990a16d9d69b6fdadd23e6a9cc7a4241a093c42264c7c570cdca60a79f1b64
MD5 fce266c71d268cd0bb55466ddadb73ee
BLAKE2b-256 97563828b0fd2c6bd4a904ca4380c1b015a38314741e0629454fae88f18da4f8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page