You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将DataFrame转换为指定结构的自定义嵌套JSON

实现方法

首先确认你的DataFrame包含prj、db、tbl、typ、con、own六列,直接使用下方代码即可完成转换:

import pandas as pd
import json

# 这里替换为你自己的DataFrame即可,下方仅为示例构造
df = pd.DataFrame([
    ["Retail", "Sales_Db", "COUNTRY", "table", "yes", "Ravi"],
    ["Finance", "rev_db", "rev_tbl", "table", "no", "Srikanth"]
], columns=["prj", "db", "tbl", "typ", "con", "own"])

# 行转换逻辑
def convert_row(row):
    return {
        "prj": row["prj"],
        "db": row["db"],
        "tbl": row["tbl"],
        "typ": row["typ"],
        "fields": {
            "con": {"type": "boolean", "value": row["con"]},
            "own": {"type": "string", "value": row["own"]}
        }
    }

# 构造最终结构并输出格式化JSON
output = {"tags": df.apply(convert_row, axis=1).tolist()}
print(json.dumps(output, indent=4, ensure_ascii=False))

如果后续需要新增fields下的字段,只需要在convert_row函数的fields字典中按对应格式添加即可。

内容的提问来源于stack exchange,提问作者kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.23 23:15:03