python - 如何从 pandas DataFrame 创建嵌套 JSON?
问题描述
我正在尝试从 DataFrame 生成嵌套的 JSON,其中汽车的属性分布在几行中。
数据框
cars = {'brand': ['Honda','Toyota','Ford','Audi','Honda','Toyota','Ford','Audi'],
'model': ['Civic','Corolla','Focus','A4','Civic','Corolla','Focus','A4'],
'attributeName': ['color','color','color','color','doors','doors','doors','doors'],
'attributeValue': ['red','blue','black','red',2,4,4,2]
}
df = pd.DataFrame(cars)
我试过的
起初我将行分组并尝试应用嵌套:
df.groupby(['brand','model'])\
.apply(lambda x: x[['attributeName','attributeValue']].to_dict('records'))\
.to_json(orient='records')
结果
[[{"attributeName":"color","attributeValue":"red"},{"attributeName":"doors","attributeValue":2}],[{"attributeName":"color","attributeValue":"black"},{"attributeName":"doors","attributeValue":4}],[{"attributeName":"color","attributeValue":"red"},{"attributeName":"doors","attributeValue":2}],[{"attributeName":"color","attributeValue":"blue"},{"attributeName":"doors","attributeValue":4}]]
预期结果
[
{
'brand':'Honda',
'model':'Civic',
'attributes':[
{
'name':'color',
'value':'red'
}
]
},
{...}
]
那么我能做些什么来获取其他记录而不仅仅是属性呢?
解决方案
在您的解决方案中rename
添加reset_index()
:
d = {'attributeName':'name','attributeValue':'value'}
j = df.rename(columns=d).groupby(['brand','model']).apply(lambda x: x[['name','value']].to_dict('records')).reset_index(name='attributes').to_json(orient='records')
print (j)
[{"brand":"Audi","model":"A4","attributes":[{"name":"color","value":"red"},{"name":"doors","value":2}]},{"brand":"Ford","model":"Focus","attributes":[{"name":"color","value":"black"},{"name":"doors","value":4}]},{"brand":"Honda","model":"Civic","attributes":[{"name":"color","value":"red"},{"name":"doors","value":2}]},{"brand":"Toyota","model":"Corolla","attributes":[{"name":"color","value":"blue"},{"name":"doors","value":4}]}]
或者:
d = {'attributeName':'name','attributeValue':'value'}
j = df.rename(columns=d).groupby(['brand','model']).apply(lambda x: x[['name','value']].to_dict('records')).explode().apply(lambda x: [x]).reset_index(name='attributes').to_json(orient='records')
print (j)
[{"brand":"Audi","model":"A4","attributes":[{"name":"color","value":"red"}]},{"brand":"Audi","model":"A4","attributes":[{"name":"doors","value":2}]},{"brand":"Ford","model":"Focus","attributes":[{"name":"color","value":"black"}]},{"brand":"Ford","model":"Focus","attributes":[{"name":"doors","value":4}]},{"brand":"Honda","model":"Civic","attributes":[{"name":"color","value":"red"}]},{"brand":"Honda","model":"Civic","attributes":[{"name":"doors","value":2}]},{"brand":"Toyota","model":"Corolla","attributes":[{"name":"color","value":"blue"}]},{"brand":"Toyota","model":"Corolla","attributes":[{"name":"doors","value":4}]}]
df['attributes'] = df.apply(lambda x: [{'name': x['attributeName'], 'value': x['attributeValue']}], axis=1)
df = df.drop(['attributeName','attributeValue'], axis=1)
print (df)
brand model attributes
0 Honda Civic [{'name': 'color', 'value': 'red'}]
1 Toyota Corolla [{'name': 'color', 'value': 'blue'}]
2 Ford Focus [{'name': 'color', 'value': 'black'}]
3 Audi A4 [{'name': 'color', 'value': 'red'}]
4 Honda Civic [{'name': 'doors', 'value': 2}]
5 Toyota Corolla [{'name': 'doors', 'value': 4}]
6 Ford Focus [{'name': 'doors', 'value': 4}]
7 Audi A4 [{'name': 'doors', 'value': 2}]
j = df.to_json(orient='records')
print (j)
[{"brand":"Honda","model":"Civic","attributes":[{"name":"color","value":"red"}]},{"brand":"Toyota","model":"Corolla","attributes":[{"name":"color","value":"blue"}]},{"brand":"Ford","model":"Focus","attributes":[{"name":"color","value":"black"}]},{"brand":"Audi","model":"A4","attributes":[{"name":"color","value":"red"}]},{"brand":"Honda","model":"Civic","attributes":[{"name":"doors","value":2}]},{"brand":"Toyota","model":"Corolla","attributes":[{"name":"doors","value":4}]},{"brand":"Ford","model":"Focus","attributes":[{"name":"doors","value":4}]},{"brand":"Audi","model":"A4","attributes":[{"name":"doors","value":2}]}]
推荐阅读
- python - 无法在 cmd 上运行 ipython
- json - 为什么 Jackson 为 @JsonSerializer/@JsonDeserializer 注释类创建每个 Serializer/Deserializer 实例
- javascript - 为什么我的 axios 并不总是在 React 中工作
- python - Pyrebase 未在 python 3.7.1 中成功安装
- bitcoin - 为什么coinbase的交易中有3个输出?
- racket - 在参数列表上调用“和”
- flutter - 如何在颤动中显示带有时区的日期和时间
- php - 如何在 VS Code 中为 Phalcon 启用 InteliSense?
- javascript - 你如何在 Discord.js 上批量创建频道
- python - 从“str”类继承