dataframe对象转换为JSON时出现问题

2024-05-12 19:02:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我´我是Python编程新手,我´我试图编写一个程序,读取xlxs文件并将其转换为json(我´我一直在使用python3和pandas的0.23.0版本,但我遇到了一些问题和困难。)

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

我的xlsx文件中有两行,每行有四列:

id     label        id_customer     label_customer

6     Sao Paulo      CUST-99992         Brazil

92    Hong Hong      CUST-88888         China

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

这是我的密码:

import pandas as pd
import json

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

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

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

print (list)

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

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

Json输出:

[
 {
  "id": 6,
  "label": "Sao Paulo",
  "Customer": [
   {
    "id": "CUST-99992",
    "label": "Brazil"
   },
   {
    "id": "CUST-88888",
    "label": "China"
   }
  ]
 },
 {
  "id": 92,
  "label": "Hong Hong",
  "Customer": [
   {
    "id": "CUST-99992",
    "label": "Brazil"
   },
   {
    "id": "CUST-88888",
    "label": "China"
   }
  ]
 }
]

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

期望值:

[
 {
  "id": 6,
  "label": "Sao Paulo",
  "Customer": [
   {
    "id": "CUST-99992",
    "label": "Brazil"
   }
  ]
 },
 {
  "id": 92,
  "label": "Hong Hong",
  "Customer": [
   {
    "id": "CUST-88888",
    "label": "China"
   }
  ]
 }
]

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

在将list1添加到列表之前,我已经尝试附加它,但没有解决问题

有人能帮我吗


Tags: idjsoncustomerlabellistrowsaohong
2条回答

你的代码几乎可以正常工作了-首先你应该检查一下。避免使用“list”这样的内置名称来表示变量-其次,顺序有点颠倒。我换了几行,最后说:

file_imported = pd.read_excel('testing.xlsx')
print(file_imported)
list1 = []
for index, row in file_imported.iterrows():
    list1.append({
            "id"       : int(row['id']),
            "label"    : str(row['label']),
            "Customer" : [{'id':str(row['id_customer']),'label':str(row['label_customer'])}]
            })

print(list1)

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

似乎和你想的一样。 顺便说一句,如果您愿意,您也可以使用df.apply来实现这一点

问题是循环末尾没有清空list1。这会导致list1在每个循环结束时的大小增加。只需在每个循环的开头清空list1即可获得所需的输出。请参阅下面的代码(我将变量list更改为list_final,因为list是一个类型,您不应该有与类型同名的变量):

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

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

输出现在是您所期望的:

print(list_final)
[
{'id': 6, 'label': 'Sao Paulo', 'Customer': [{'id': 'CUST-99992', 'label': 'Brazil'}]}, 
{'id': 92, 'label': 'Hong Kong', 'Customer': [{'id': 'CUST-88888', 'label': 'China'}]}
]


相关问题 更多 >