【问题标题】:How to convert Pandas Dataframe to Nested Json Output如何将 Pandas Dataframe 转换为嵌套的 Json 输出
【发布时间】:2021-11-16 01:03:45
【问题描述】:

我不熟悉将 pandas DataFrames 转换为 JSON 格式。

我有一个 DataFrame 的输入:

a1       a2      a3      a4  a5
agent1   abc     quote1  NJ  19029
agent1   abc     quote1  NJ  19029
agent2   xyz     quote2  CA  95003
agent2   xyz     quote2  CA  95003

这里的每一行代表每个代理的报价行。行的重复是故意的。

我正在寻找以下 JSON 格式的输出:

{
        "quotes": [
            {
                "agents": [
                    {
                        "a1": "agent1",
                        "a2": "abc",
                        "a3": "quote1",
                        "a4": "NJ",
                        "a5": "19029"
                    },
                    {
                        "a1": "agent1",
                        "a2": "abc",
                        "a3": "quote1",
                        "a4": "NJ",
                        "a5": "19029"
                    }
                ]
            },
            {
                "agents": [
                    {
                        "a1": "agent2",
                        "a2": "xyz",
                        "a3": "quote2",
                        "a4": "CA",
                        "a5": "95003"
                    },
                    {
                      "a1": "agent2",
                        "a2": "xyz",
                        "a3": "quote2",
                        "a4": "CA",
                        "a5": "95003"
                    }
                ]
            }
        ]}

【问题讨论】:

    标签: arrays json pandas nested python-3.6


    【解决方案1】:

    您格式化输出的方式不是 Pandas to_json 输出直接支持的方式。它需要对 DataFrame 进行一些重组:

    # Create dictionaries of the records for the groups of quotes and agents
    grouped = df.groupby(["a3", "a1"]).apply(lambda x: x.to_dict(orient="records"))
    
    # Build the final dictionary:
    quotes_dict = {"quotes": [{"agents": x} for x in grouped]}
    
    # Create json
    json.dumps(quotes_dict, indent=4)
    

    创建此输出:

    {
        "quotes": [
            {
                "agents": [
                    {
                        "a1": "agent1",
                        "a2": "abc",
                        "a3": "quote1",
                        "a4": "NJ",
                        "a5": 19029
                    },
                    {
                        "a1": "agent1",
                        "a2": "abc",
                        "a3": "quote1",
                        "a4": "NJ",
                        "a5": 19029
                    }
                ]
            },
            {
                "agents": [
                    {
                        "a1": "agent2",
                        "a2": "xyz",
                        "a3": "quote2",
                        "a4": "CA",
                        "a5": 95003
                    },
                    {
                        "a1": "agent2",
                        "a2": "xyz",
                        "a3": "quote2",
                        "a4": "CA",
                        "a5": 95003
                    }
                ]
            }
        ]
    }
    

    【讨论】:

      猜你喜欢
      • 2019-06-09
      • 2014-06-27
      • 1970-01-01
      • 2021-03-07
      • 2020-09-29
      • 1970-01-01
      • 1970-01-01
      • 2021-12-09
      • 2021-12-29
      相关资源
      最近更新 更多