简体   繁体   English

如何将 pandas dataframe 转换为嵌套 JSON ZA8CFDE6331BD59EB66AC96F8911C4?

[英]How do I convert pandas dataframe to nested JSON object?

I have an SQL database that I need to fetch and convert to JSON.我有一个 SQL 数据库,我需要获取并转换为 JSON。 I am thinking that the first step to do that is to fetch the data from the database and load it as a dataframe, then convert the dataframe into JSON object. I am thinking that the first step to do that is to fetch the data from the database and load it as a dataframe, then convert the dataframe into JSON object.

Let's say I have the following dataframe.假设我有以下 dataframe。

df_school = pd.DataFrame({'id':[1,2,3,4], 'school_code': ['ABC', 'IJK', 'QRS', 'XYZ'], 'name': ['School A','School B', 'School C', 'School D'], 'type':['private', 'public', 'public', 'private']})
print(df_school)

I want to convert it to JSON with the following code.我想使用以下代码将其转换为 JSON。

import collections

object_list =[]
for idx, row in df_school.iterrows():
    d = collections.OrderedDict()
    d['id'] = row['id']
    d['school_code'] = row['school_code']
    d['name'] = row['name']
    d['type'] = row['type']
    object_list.append(d)

j = json.dumps(object_list)
object_list = 'school_objects.js'
f = open(object_list, 'w')
print(j)

But the result is string.但结果是字符串。 It only looks like a JSON, but when I try to access the item inside the so-called JSON, like j[0] it prints [ , not an item inside the JSON.它只看起来像 JSON,但是当我尝试访问所谓的 JSON 内的项目时,就像j[0]一样,它会打印[ ,而不是 Z0ECD11C1D7A287421D148A23BBD7 内的项目。

I also tried another approach, by converting the result from SQL directly to JSON.我还尝试了另一种方法,将结果从 SQL 直接转换为 JSON。

query = "Select * from school;"
df_school = pd.read_sql_query(query, connection)
json_school = df_school.head(10).to_json(orient='records')

But I also still got string.但我也得到了字符串。

How do I convert it to real JSON object?如何将其转换为真正的 JSON object?

Given the provided df_school variable, we can just do j=df_school.to_json(orient='records') to turn it into a JSON formatted string.鉴于提供的df_school变量,我们只需执行j=df_school.to_json(orient='records')将其转换为 JSON 格式的字符串。

Once we have j storing the JSON formatted string, if we want to do something with it, we first have to load the JSON into Python again using json.loads(j) . Once we have j storing the JSON formatted string, if we want to do something with it, we first have to load the JSON into Python again using json.loads(j) . So if we do:所以如果我们这样做:

j = df_school.to_json(orient='records')
# parse j into Python
loaded_json = json.loads(j)
print(loaded_json[0])
# print outputs: {'id': 1, 'name': 'School A', 'school_code': 'ABC', 'type': 'private'}

Hope this helps!希望这可以帮助!

import pandas as pd
import json
df_school = pd.DataFrame({'id':[1,2,3,4], 'school_code': ['ABC', 'IJK', 'QRS', 'XYZ'], 'name': ['School A','School B', 'School C', 'School D'], 'type':['private', 'public', 'public', 'private']})
str_school = df_school.to_json(orient='records')
json_school = json.loads(str_school)
json_school[0]

{'id': 1, 'school_code': 'ABC', 'name': 'School A', 'type': 'private'} {'id': 1, 'school_code': 'ABC', 'name': 'School A', 'type': 'private'}

JSON is a string encoding of objects. JSON 是对象的字符串编码。

Once you use json.dumps() or similar, you'll get a string.一旦你使用json.dumps()或类似的,你会得到一个字符串。

Try the below code, Hope this will help:试试下面的代码,希望这会有所帮助:

data = [{columns:df_school.iloc[i][columns] for columns in list(df_school.columns)  }  for i in range(df_school.shape[0])   ]

print(data)
print("***********************")
print(type(data[0]))

Ouput will be:输出将是:

[{'id': 1, 'school_code': 'ABC', 'name': 'School A', 'type': 'private'},
 {'id': 2, 'school_code': 'IJK', 'name': 'School B', 'type': 'public'},
 {'id': 3, 'school_code': 'QRS', 'name': 'School C', 'type': 'public'},
 {'id': 4, 'school_code': 'XYZ', 'name': 'School D', 'type': 'private'}]

*************************
<class 'dict'>
data={k:list(v.values()) for k,v in df_school.to_dict().items()}
{
'id': [1, 2, 3, 4],
'school_code': ['ABC', 'IJK', 'QRS', 'XYZ'],
'name': ['School A', 'School B', 'School C', 'School D'],
'type': ['private', 'public', 'public', 'private']
}

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

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