python - 如何将熊猫数据框转换为嵌套的 json
问题描述
目前有一个包含笔记本电脑信息的数据框,目的是将数据转换为嵌套的 json 结构。由于笔记本电脑的品牌、价格和重量是相关信息,因此想将它们归为笔记本电脑字段下。任何有关如何转换数据帧的指针将不胜感激。
data frame
Target json structure
解决方案
考虑df
:
In [2729]: df = pd.DataFrame({'Store code':[1, 12, 132], 'Laptop brand':['Lenovo', 'Apple', 'HP'], 'Laptop price':[1000, 2000, 1200], 'Laptop weight':[1.2, 1.5, 1.4], 'star':[3, 5, 4]})
In [2730]: df
Out[2730]:
Store code Laptop brand Laptop price Laptop weight star
0 1 Lenovo 1000 1.2 3
1 12 Apple 2000 1.5 5
2 132 HP 1200 1.4 4
df._to_dict
与 一起使用orient='records'
:
In [2734]: x = df.to_dict('records') # Create a list of dicts from df
In [2738]: l = [] # Empty list for storing your output
In [2763]: for i in x: # Iterate every item in list of dicts
...: d = {}
...: d1 = {}
...: for k,v in i.items(): # Iterate each individual dict
...: if 'Laptop' in k: # Check if word `Laptop` is present in key
...: d1[k] = v # If yes, create a separate dict for all laptop keys
...: else:
...: d[k] = v
...: d['Laptop'] = [d1] # Add another key `Laptop` which holds a `list` of dicts of Laptop
...: l.append(d) # Append this dict in list
...:
输出:
In [2774]: print(json.dumps(l, indent=2))
[
{
"Store code": 1,
"star": 3,
"Laptop": [
{
"Laptop brand": "Lenovo",
"Laptop price": 1000,
"Laptop weight": 1.2
}
]
},
{
"Store code": 12,
"star": 5,
"Laptop": [
{
"Laptop brand": "Apple",
"Laptop price": 2000,
"Laptop weight": 1.5
}
]
},
{
"Store code": 132,
"star": 4,
"Laptop": [
{
"Laptop brand": "HP",
"Laptop price": 1200,
"Laptop weight": 1.4
}
]
}
]