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

如何使用Pandas从多行属性的DataFrame生成聊天机器人嵌套JSON

从多行DataFrame生成嵌套JSON的解决方案

步骤1:明确DataFrame结构

假设你的DataFrame包含三列:tag(意图标签)、type(内容类型,分为pattern和response)、content(具体内容文本)。如果你的列名不同,替换成对应名称即可。

步骤2:分组聚合数据

用pandas按tag分组,分别收集每个标签下的提问和回复列表:

import pandas as pd
import json

# 替换成你的实际DataFrame
data = {
    "tag": ["Glacier cave"]*8,
    "type": ["pattern"]*4 + ["response"]*4,
    "content": [
        "how are glacier caves formed",
        "what is glacier cave?",
        "give me info on glacier caves",
        "where is glacier cave formed?",
        "A partly submerged glacier cave on Perito Moreno Glacier",
        "The ice facade is approximately 60 m high",
        "A glacier cave is a cave formed within the ice of a glacier",
        "Glacier caves are often called ice caves , but this term is properly used to describe bedrock caves that contain year-round ice"
    ]
}
df = pd.DataFrame(data)

# 构造目标结构
intents_data = []
for tag, group in df.groupby("tag"):
    patterns = group[group["type"] == "pattern"]["content"].tolist()
    responses = group[group["type"] == "response"]["content"].tolist()
    intents_data.append({
        "tag": tag,
        "patterns": patterns,
        "responses": responses
    })

result = {"intents": intents_data}

步骤3:输出或保存JSON

要打印格式化的JSON:

print(json.dumps(result, indent=4))

要保存到文件:

with open("intents.json", "w", encoding="utf-8") as f:
    json.dump(result, f, indent=4, ensure_ascii=False)

最终生成的JSON

运行后会得到你期望的结构:

{
    "intents": [
        {
            "tag": "Glacier cave",
            "patterns": [
                "how are glacier caves formed",
                "what is glacier cave?",
                "give me info on glacier caves",
                "where is glacier cave formed?"
            ],
            "responses": [
                "A partly submerged glacier cave on Perito Moreno Glacier",
                "The ice facade is approximately 60 m high",
                "A glacier cave is a cave formed within the ice of a glacier",
                "Glacier caves are often called ice caves , but this term is properly used to describe bedrock caves that contain year-round ice"
            ]
        }
    ]
}

注意事项

  • 如果你的DataFrame结构不同(比如没有type列,而是用单独的pattern/response列,每行仅一个有效内容),可调整聚合逻辑:用dropna过滤空值后收集列表。
  • 处理中文时,需指定encoding="utf-8"和ensure_ascii=False避免乱码。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 03:30:50