使用嵌套数组将数据帧转换为 json

问题描述 投票:0回答:1

我是 python 和 pandas 的新手,在将数据帧转换为带有嵌套数组的 json 格式时遇到问题

我的数据框如下:

颜色

我正在尝试以以下格式创建 json 输出,其中每个类型和颜色数组都有嵌套的详细信息数组:

{
  "types": [
    {
      "A": [
        {
          "colour": "RED",
          "code": "R",
          "details": [
            {
              "id": "11a",
              "value": "166.23"
            },
            {
              "id": "12b",
              "value": "999.99"
            }
          ]
        },
        {
          "colour": "BLUE",
          "code": "B",
          "details": [
            {
              "id": "44a",
              "value": "12.33"
            }
          ]
        }
      ],
      "B": [
        {
          "colour": "RED",
          "code": "R",
          "details": [
            {
              "id": "66y",
              "value": "12345.00"
            }
          ]
        }
      ]
    }
  ]
}

我尝试过按类型和颜色分组,但没有得到每种类型的数组 我也不知道如何从我的数据框中获取包含 id 和 value 列标题的详细信息数组。

json 输出给出:

[{"type":"A","colour":"BLUE","code":"B","details":[["44a",12.33]]},{"type":"A","colour":"RED","code":"R","details":[["11a",166.23],["12b",999.99]]},{"type":"B","colour":"RED","code":"R","details":[["66y",12345.0]]}]

字典给出:

{('A', 'BLUE'): {'code': 'B', 'details': [['44a', 12.33]]}, ('A', 'RED'): {'code': 'R', 'details': [['11a', 166.23], ['12b', 999.99]]}, ('B', 'RED'): {'code': 'R', 'details': [['66y', 12345.0]]}}

我不确定这是否是获得我想要的 json 的正确方向。

到目前为止给出的代码是上面的 json 和字典:

` df = pd.read_excel("/Colours.xlsx",sheet_name = '颜色',index_col =[0])

df['details'] = df.apply(lambda x: list([x['id'],x['value']]),axis=1)
df_grouped = df.groupby(["type", "colour","code"]).details.agg(list).reset_index()
output = df_grouped.to_json(orient='records')

df_grouped = df_grouped.set_index(['type', 'colour'])
df_dict= df_grouped.to_dict(orient='index')`
python pandas dataframe
1个回答
0
投票

此代码提供与您请求的输出类似的输出。

代码

import pandas as pd

list_colors = []
list_colors.append(["A","RED","R","11a",166.23])
list_colors.append(["A","RED","R","12a",999.99])
list_colors.append(["A","BLUE","B","44a",12.33])
list_colors.append(["B","RED","R","66y",12345.00])

df = pd.DataFrame(list_colors,columns=["type","colour","code","id","value"])

nested_df = (df.groupby(['type','colour','code'])
       .apply(lambda x: x[['id','value']].to_dict('records'), include_groups=False)
       .reset_index()
       .rename(columns={0:'details'})
       .groupby(['type'])
       .apply(lambda x: x[['colour','code','details']].to_dict('records'), include_groups=False)
)

parent_df = pd.DataFrame({
    'types': nested_df
})

parent_df = parent_df.to_json()

parent_df

输出

'{"types":{"A":[{"colour":"BLUE","code":"B","details":[{"id":"44a","value":12.33}]},{"colour":"RED","code":"R","details":[{"id":"11a","value":166.23},{"id":"12a","value":999.99}]}],"B":[{"colour":"RED","code":"R","details":[{"id":"66y","value":12345.0}]}]}}'

这篇文章很有用: 将 Pandas Dataframe 转换为嵌套 JSON

© www.soinside.com 2019 - 2024. All rights reserved.