我需要使用pandas模块解析和平铺python中的嵌套json
源JSON:
{
"people": [{
"name": "ABC",
"age": "33",
"mobile": "44545",
"location": "hyderabad",
"interests": [{
"hobby": "dancing",
"food": "continental",
"city": "Paris"
}]
},
{
"name": "DEF",
"age": "11",
"mobile": "12121212",
"location": "pune",
"interests": [{
"hobby": "reading",
"food": "Pizza",
"city": "France"
}]
}
]
}
从上面的源json文件中,我需要获得两个不同的json文件,如下所示:
输出JSON 1:
{"name": "ABC", "age": "33", "mobile": "44545", "location": "hyderabad"}
{"name": "DEF", "age": "11","mobile": "12121212", "location": "pune”}
输出JSON 2:
{"name": "ABC”, ”interests_hobby”:”dancing”, “interests_food”:”continental”, “interests_city”:”Paris”}
{“name": "DEF”, ”interests_hobby”:”reading”, “interests_food”:”Pizza”, “interests_city”:”France”}
条件是我们必须使用python和pandas模块
首先,您必须打开json文件并加载它。然后,使用命令
df = pd.json_normalize(json_obj, record_path="people")
获取包含所需字段的数据帧。如您所见,“兴趣”字段尚未拆分,但您可以重做这些步骤
为了获得具有所需输出的json文件,我建议您
df.to_json(orient='records')
返回一个json文件,其中包含来自数据帧的所有索引
相关问题 更多 >
编程相关推荐