python - Pandas 数据框和附加对象转换为 JSON

标签 python json pandas dataframe if-statement

基本上我正在使用 pandas 读取 xlsx 文件并将其转换为 json 文件。我不知道该怎么做,但我认为我必须创建一个“if”语句来读取每一行并查找哪些元素与前一行不同,然后将其附加到我的对象中。

我正在阅读的数据:

id     label        id_customer     label_customer    part_number 

6     Sao Paulo      CUST-99992         Brazil          7897

6     Sao Paulo      CUST-99992         Brazil          982

6     Sao Paulo      CUST-43535         Brazil          435

92    Hong Hong      CUST-88888         China           785

==================================

这是我的代码:

import pandas as pd
import json

file_imported = pd.read_excel('testing.xlsx', sheet_name = 'Plan1')

list_final  = []
for index, row in file_imported.iterrows():
    list1 = []
    list_final.append ({
        "id"       : int(row['id']),
        "label"    : str(row['label']),
        "Customer" : list1
        })

    list2 = []
    list1.append ({
       "id"       : str(row['id_customer']) ,
       "label"    : str(row['label_customer']),
       "number"   :  list2
       })

    list2.append({
        "part"    : str(row['part_number'])  
       })      

print (list_final)

with open ('testing.json', 'w') as f:
    json.dump(list_final, f, indent= True)

==================================

Json 输出:

    [
     {
      "id": 6,
      "label": "Sao Paulo",
      "Customer": [
       {
        "id": "CUST-99992",
        "label": "Brazil",
        "number" : [
        {
        "part": "7897"
        }
        ]
       }
      ]
     },
     {
      "id": 6,
      "label": "Sao Paulo",
      "Customer": [
       {
        "id": "CUST-99992",
        "label": "Brazil",
        "number" : [
        {
        "part": "982"
        }
        ]
       }
      ]
     },
     {
      "id": 6,
      "label": "Sao Paulo",
      "Customer": [
       {
        "id": "CUST-43535",
        "label": "Brazil",
        "number" : [
        {
        "part": "435"
        }
        ]
       }
      ]
     },
     {
      "id": 92,
      "label": "Hong Hong",
      "Customer": [
       {
        "id": "CUST-88888",
        "label": "China",
        "number" : [
        {
        "part": "785"
        }
        ]
       }
      ]
     }
    ]  

==================================

我需要这样的东西:

[
 {
  "id": 6,
  "label": "Sao Paulo",
  "Customer": [
   {
    "id": "CUST-99992",
    "label": "Brazil",
    "number" : [
    {
    "part": "7897"
    },
    {
    "part": "982"
    }
    ]
   },
   {
    "id": "CUST-43535",
    "label": "Brazil",
    "number" : [
    {
    "part": "435"
    }
    ]
   }
  ]
 },
 {
  "id": 92,
  "label": "Hong Hong",
  "Customer": [
   {
    "id": "CUST-88888",
    "label": "China",
    "number" : [
    {
    "part": "785"
    }
    ]
   }
  ]
 }
]

======================

有人可以帮我吗???

最佳答案

查看您想要的 json,它分为两组。第一个包含 idlabel 字段,第二个包含 id_customerlabel_customer 字段。最里面的数据是part_number,它可以使用列表理解和字典理解来创建:[{'part': str(p)} for p in df2['part_number ']]。剩下的只是数据处理。

import json

result = []
for labels, df1 in df.groupby(['id', 'label']):
    id_, label = labels
    record = {'id': int(id_), 'label': label, 'Customer': []}
    for inner_labels, df2 in df1.groupby(['id_customer', 'label_customer']):
        id_, label = inner_labels
        record['Customer'].append({
            'id': id_, 
            'label': label, 
            'number': [{'part': str(p)} for p in df2['part_number']]
        })
    result.append(record)
>>> print(json.dumps(result, indent=True))
[
 {
  "id": 6,
  "label": "Sao Paulo",
  "Customer": [
   {
    "id": "CUST-43535",
    "label": "Brazil",
    "number": [
     {
      "part": "435"
     }
    ]
   },
   {
    "id": "CUST-99992",
    "label": "Brazil",
    "number": [
     {
      "part": "7897"
     },
     {
      "part": "982"
     }
    ]
   }
  ]
 },
 {
  "id": 92,
  "label": "Hong Kong",
  "Customer": [
   {
    "id": "CUST-88888",
    "label": "China",
    "number": [
     {
      "part": "785"
     }
    ]
   }
  ]
 }
]

关于python - Pandas 数据框和附加对象转换为 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59200772/

相关文章:

python - 在 Matplotlib 中的极坐标图上设置 xlim、ylim

python - scipy 的曲线拟合不适用于正弦曲线?

python - 将 JSON 数据发送到客户端 django

python - 如何在 Python (Pandas) 中重命名特定范围的列

python - 如何优雅地忽略 Python 函数的某些返回值?

python - 基于汉明距离从Python列表中删除字符串

javascript - 为什么当我传入不同的对象时,AJAX 会向回调返回相同的数据?

sql - 有什么办法可以将下面嵌套的json数据插入到sql server中

python - pandas 根据条件执行操作 - 不同的方式和最佳实践?

python - Pandas,应用函数,它为两行接受两个参数