繁体   English   中英

如何将 pandas df 转换为嵌套的 json

[英]How to convert a pandas df into a nested json

我正在尝试将 pandas dataframe 转换为嵌套的 json,但卡住了。 Dataframe 包含以下格式的数据:

   description         69    70   project_id    60
1  Lorem Ipsum...            14     5679        CA   
2  Lorem Ipsum...      hom   15     2904        CA  
3  Lorem Ipsum...      im    14     5679        CA       

我想要的是一个 JSON,看起来像这样:

    [ {
  "issue": {
      "project_id": 5679,
      "description": "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.",
      "custom_fields": [
        {
          "id": 69,
          "value": null
        },
        {
          "id": 60,
          "value": "CA"
        },
        {
          "id": 70,
          "value": "14"
        }
      ]
    }
  },
  {
  "issue": {
      "project_id": 2904,
      "description": "Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet ",
      "custom_fields": [
        {
          "id": 69,
          "value": "hom"
        },
        {
          "id": 60,
          "value": "CA"
        },
        {
          "id": 70,
          "value": "15"
        }
      ]
    }
  },

这是我到目前为止所拥有的,但它不会产生所需的格式,例如我没有得到的是 JSON 的标题(“问题”...)和嵌套(“id”:. .., “价值”: ...)

j = (df_test.groupby(['description','project_id'])
       .apply(lambda x: x[['69', '70', '84', '60']].to_dict('records'))
       .reset_index()
       .rename(columns={0:'custom_fields'})
       .to_json(orient='records'))

关于我必须调整的任何想法? 提前感谢任何帮助和提示

我建议简单地遍历数据帧并根据需要处理信息,而不是将其置于一个链式 function 调用中:

import pandas as pd
import json

# create example data
df = pd.DataFrame({"description" : ['Lorem Ipsum', 'Lorem Ipsum', 'Lorem Ipsum'],
                   69: [None, "hom", "im"],
                   70: [14, 15, 14],
                   "project_id" : [5679, 2904, 5679],
                   60: ["CA", "CA", "CA"]
                   
                  })


# convert data to desired format
res = []
custom_field_keys = [69, 60, 70]

for idx, row in df.iterrows():
    entry = { key: value for key, value in row.items() if key not in custom_field_keys }
    entry["custom_fields"] = [
        {"id": key, "value": value} for key, value in row.items() if key in custom_field_keys
    ]
    res.append({"issue": entry})


print(json.dumps(res, indent=2))

这产生:

[
  {
    "issue": {
      "description": "Lorem Ipsum",
      "project_id": 5679,
      "custom_fields": [
        {
          "id": 69,
          "value": null
        },
        {
          "id": 70,
          "value": 14
        },
        {
          "id": 60,
          "value": "CA"
        }
      ]
    }
  },
  {
    "issue": {
      "description": "Lorem Ipsum",
      "project_id": 2904,
      "custom_fields": [
        {
          "id": 69,
          "value": "hom"
        },
        {
          "id": 70,
          "value": 15
        },
        {
          "id": 60,
          "value": "CA"
        }
      ]
    }
  },
  {
    "issue": {
      "description": "Lorem Ipsum",
      "project_id": 5679,
      "custom_fields": [
        {
          "id": 69,
          "value": "im"
        },
        {
          "id": 70,
          "value": 14
        },
        {
          "id": 60,
          "value": "CA"
        }
      ]
    }
  }
]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM