使用最里面的对象层将pandas DataFrame转换为深度嵌套的JSON [英] Convert pandas DataFrame to deeply nested JSON with an innermost object layer

查看:76
本文介绍了使用最里面的对象层将pandas DataFrame转换为深度嵌套的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个DataFrame df 像:

Assume I have a DataFrame df like:

source      tables      columns   data_type   length
src1        table1      col1      INT         4
src1        table1      col2      CHAR        2
src1        table2      col1      CHAR        2
src2        table1      col1      INT         4
src2        table1      col2      DATE        3

注意:DataFrame还有另外4个与问题无关的列

NOTE: the DataFrame also has another 4 columns which aren't relevant to the problem

需要类似于以下内容的输出:

Need an output that looks similar to:

{
  "src1": {
    "table1": {
      "col1": {
        "type": "INT"
        "length": 4
      },
      "col2": {
        "type": "CHAR"
        "length": 2
      }
    },
    "table2": {
      "col1": {
        "type": "CHAR"
        "length": 2
      }
    }
  },
  "src2": {
    "table1": {
      "col1": {
        "type": "INT"
        "length": 4
      },
      "col2": {
        "type": "DATE"
        "length": 3
      }
    }
  }
}

我目前拥有的代码会产生与上述相同的输出,但不包括实际的数据类型值(即而不是 type: CHAR ,我得到的是 type: )我不确定我如何能够相应地嵌套值。以下是代码:

The code the I currently have produces the same output as above with the exclusion of the actual data type values (ie. instead of "type": "CHAR", I'm getting "type": "") as I'm not sure how I'd be able to nest the values accordingly. Here is the code:

def make_nested(df): 
        f = lambda: defaultdict(f)   
        data = f()  

        for row in df.to_numpy().tolist():
            t = data
            for r in row[:-6]:
                t = t[r]
            t[row[-6]] = {
                "type": '',
                "length": ''
            }

        return data

我的问题是如何正确附加 data_type length 列值到每个 JSON对象中而不牺牲确切格式?谢谢。

My question is how can I properly append the data_type and length column values into each columns JSON object without sacrificing the exact format? Thanks.

推荐答案

def make_nested(df): 
    f = lambda: defaultdict(f)   
    data = f()  

    for row in df.to_numpy().tolist():
        t = data
        for r in row[:-3]:
            t = t[r]
        t[row[-3]] = {
            "type": row[-2],
            "length": row[-1]
        }

    return data

最后两列值进入第三级,这就是您应该做的。

The last two column values go inside the third level, so thats what you should do.

这篇关于使用最里面的对象层将pandas DataFrame转换为深度嵌套的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆