分析JSON时python pandas TypeError:字符串索引必须是整数

2024-10-02 00:40:12 发布

您现在位置:Python中文网/ 问答频道 /正文

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中的一个例子


Tags: inidvaluewithgroupdescriptionskinunits
3条回答

代码运行得非常好,但是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}]}]

这是一个只有一条记录的例子。在

阿马丹回答了这个问题,但在看到他的答案之前,我设法解决了这个问题:

for i in range(len(db)):
    rec = db.loc[i]
    fnuts = pd.DataFrame(rec["nutrients"])
    fnuts["id"] = rec["id"]
    nutrients.append(fnuts)

for rec in db遍历列名。迭代行

for id, rec in db.iterrows():
    fnuts = pd.DataFrame(rec["nutrients"])
    fnuts["id"] = rec["id"]
    nutrients.append(fnuts)

这有点慢(所有需要构造的dicts)。itertuples更快;但由于您只关心两个序列,直接迭代序列可能最快:

^{pr2}$

相关问题 更多 >

    热门问题