创建一个来自 pandas 的唯一的 JSON 对象。

huangapple go评论75阅读模式
英文:

Creating a unique json object from pandas

问题

我想创建一个JSON对象来表示这个数据框,如下所示:

{
    "median_home_value": [
        {"year": 2010, "value": 11973, "type": "analyzed"},
        {"year": 2011, "value": 12500, "type": "analyzed"},
        {"year": 2012, "value": 13325, "type": "analyzed"},
        {"year": 2013, "value": 14204, "type": "analyzed"},
        {"year": 2014, "value": 14815, "type": "analyzed"},
        {"year": 2015, "value": 15393, "type": "analyzed"},
        {"year": 2016, "value": 15901, "type": "analyzed"},
        {"year": 2017, "value": 16680, "type": "analyzed"},
        {"year": 2018, "value": 17497, "type": "analyzed"},
        {"year": 2019, "value": 18249, "type": "analyzed"},
        {"year": 2020, "value": 19381, "type": "analyzed"},
        {"year": 2021, "value": 20292, "type": "analyzed"},
        {"year": 2027, "value": 20718.132, "type": "predicted"},
        {"year": 2024, "value": 21225.432, "type": "predicted"},
        {"year": 2026, "value": 21103.68, "type": "predicted"},
        {"year": 2025, "value": 20779.008, "type": "predicted"},
        {"year": 2023, "value": 20941.344, "type": "predicted"}
    ]
}

您可以尝试以下代码来实现这个JSON对象的创建:

import pandas as pd
import json

# 创建一个新的DataFrame,包含所需的列
new_df = test_df[['Year', 'Median_Home_Value', 'type']].rename(columns={'Year': 'year', 'Median_Home_Value': 'value', 'type': 'type'})

# 转换DataFrame为JSON对象
json_data = new_df.to_json(orient='records')

# 创建最终的JSON对象
result = {
    "median_home_value": json.loads(json_data)
}

# 将result对象转换为JSON字符串
result_json = json.dumps(result, indent=4)
print(result_json)

这段代码将创建一个与您所期望的JSON对象结构相匹配的JSON字符串。

英文:

I have this dataframe

 	   Year 	type 	    Median_Home_Value
786252 	2010 	analyzed 	11973.000
786253 	2011 	analyzed 	12500.000
786254 	2012 	analyzed 	13325.000
786255 	2013 	analyzed 	14204.000
786256 	2014 	analyzed 	14815.000
786257 	2015 	analyzed 	15393.000
786258 	2016 	analyzed 	15901.000
786259 	2017 	analyzed 	16680.000
786260 	2018 	analyzed 	17497.000
786261 	2019 	analyzed 	18249.000
786262 	2020 	analyzed 	19381.000
786263 	2021 	analyzed 	20292.000
899389 	2027 	predicted 	20718.132
899390 	2024 	predicted 	21225.432
899397 	2026 	predicted 	21103.680
899415 	2025 	predicted 	20779.008
899481 	2023 	predicted 	20941.344

I want to create a json object to represent this dataframe to be like this

{

	median_home_value: [{year: 2010, value: 11973, type: analyzed}, {year: 2011, value: 12500, type: analyzed}, ....]

}

I tried to do something like this

d = {}
d['Median_Home_Value] = test_df[['Year', 'Median_Home_Value', 'type']].to_json()

but it does not give me the expected result, any suggestions are appreciated.

答案1

得分: 1

{
"将DataFrame转换为字典表示,然后将记录转储为JSON数据:": {
"示例代码": "import json\n\nrecords = df.rename(columns={'Median_Home_Value': 'value'}).to_dict('records')\njson_data = json.dumps({'median_home_value': records})",
"结果": {
"median_home_value": [
{
"Year": 2010,
"type": "analyzed",
"value": 11973.0
},
{
"Year": 2011,
"type": "analyzed",
"value": 12500.0
},
{
"Year": 2012,
"type": "analyzed",
"value": 13325.0
},
...
]
}
}
}

英文:

Convert the dataframe to dictionary representation then dump the records into json data:

import json

records = df.rename(columns={'Median_Home_Value': 'value'}).to_dict('records')
json_data = json.dumps({'median_home_value': records})

Result

{
    "median_home_value": [
        {
            "Year": 2010,
            "type": "analyzed",
            "value": 11973.0
        },
        {
            "Year": 2011,
            "type": "analyzed",
            "value": 12500.0
        },
        {
            "Year": 2012,
            "type": "analyzed",
            "value": 13325.0
        },
        ...
}

huangapple
  • 本文由 发表于 2023年7月20日 13:28:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76726911.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定