在JSON文件中的記錄是這樣的(請注意什麼「營養素」的樣子):蟒蛇大熊貓 - 類型錯誤解析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}
...
]
}
下面是從代碼書本練習*。它包括了一些爭論和每個食品營養組合成一個單一的大表:
import pandas as pd
import json
db = pd.read_json("foods-2011-10-03.json")
nutrients = []
for rec in db:
fnuts = pd.DataFrame(rec["nutrients"])
fnuts["id"] = rec["id"]
nutrients.append(fnuts)
不過,我得到以下錯誤,我想不通爲什麼:
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
*這是書中的一個例子
你JSON無效(甚至當更正引號並刪除這些點時,它不能由'pd.read_json'加載)。請提交我們實際可以看到您的問題的數據。 – Amadan
@Amadan,這裏是鏈接到數據:https://github.com/wesm/pydata-book/blob/master/ch07/foods-2011-10-03.json –