python代码--利用python进行数据分析里第十四章缺失代码

问题:书上P413页中缺少一部分代码,否则无法做下面的分析

解决思路:根据书中提示,先建立一个空表,再对每个二层数据提取后连接在一起,不过我这里没有使用concat

代码如下:

nutrients = pd.DataFrame()
for i in range(0,len(db)):
    summrize=pd.DataFrame(db[i]['nutrients'],columns=['description','group','units','value','id'])
    summrize['id']=db[i]['id']
    nutrients = nutrients.append(summrize)
nutrients

注意,行号虽然显示奇怪,但数据都在且无问题,但需要运行7分钟左右

 descriptiongroupunitsvalueid
0 Protein Composition g 25.180 1008
1 Total lipid (fat) Composition g 29.200 1008
2 Carbohydrate, by difference Composition g 3.060 1008
3 Ash Other g 3.280 1008
4 Energy Energy kcal 376.000 1008
... ... ... ... ... ...
45 Vitamin B-12, added Vitamins mcg 0.000 43546
46 Cholesterol Other mg 0.000 43546
47 Fatty acids, total saturated Other g 0.072 43546
48 Fatty acids, total monounsaturated Other g 0.028 43546
49 Fatty acids, total polyunsaturated Other g 0.041 43546

389355 rows × 5 columns

原文地址:https://www.cnblogs.com/xurui89/p/12052999.html