JSON文件中的记录如下所示(请注意“营养素”是什么样子):
{
"id": 21441,
"description": "KENTUCKY FRIED CHICKEN, Fried Chicken, EXTRA CRISPY,
Wing, meat and skin with breading",
"tags": ["KFC"],
"manufacturer": "Kentucky Fried Chicken",
"group": "Fast Foods",
"portions": [
{
"amount": 1,
"unit": "wing, with skin",
"grams": 68.0
},
...
],
"nutrients": [
{
"value": 20.8,
"units": "g",
"description": "Protein",
"group": "Composition"
},
{'description': 'Total lipid (fat)',
'group': 'Composition',
'units': 'g',
'value': 29.2}
...
]
}
以下是练习*一书中的代码。它包括一些争吵,并将每种食物的营养成分汇集到一张大桌子上:
^{pr2}$但是,我得到了以下错误,我不知道为什么:
TypeError Traceback (most recent call last)
<ipython-input-23-ac63a09efd73> in <module>()
1 for rec in db:
----> 2 fnuts = pd.DataFrame(rec["nutrients"])
3 fnuts["id"] = rec["id"]
4 nutrients.append(fnuts)
5
TypeError: string indices must be integers
*这是书Python for Data Analysis中的一个例子
代码运行得非常好,但是
json
应该如下所示:这是一个只有一条记录的例子。在
阿马丹回答了这个问题,但在看到他的答案之前,我设法解决了这个问题:
for rec in db
遍历列名。迭代行这有点慢(所有需要构造的dicts)。
^{pr2}$itertuples
更快;但由于您只关心两个序列,直接迭代序列可能最快:相关问题 更多 >
编程相关推荐