Upcoming SlideShare
×

# Python pandas workshop iPython notebook (163 pages)

1,524 views
1,426 views

Published on

3 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
1,524
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
0
0
Likes
3
Embeds 0
No embeds

No notes for slide

### Python pandas workshop iPython notebook (163 pages)

1. 1. 26/01/2013 08:36 In [2]: print hello, world! Download notebook plt.plot(randn(1000).cumsum()) hello, world! Out[2]: [<matplotlib.lines.Line2D at 0x10a566c50>] In [ ]: import numpy as np import pandas as pd import simplejson as json db_path = foods-2011-10-03.json In [3]: a = [1, 2, 3, 4, 5] In [6]: import numpy as np In [7]: arr = np.array(a) arr Out[7]: array([1, 2, 3, 4, 5]) In [16]: sum(a) Out[16]: 15 In [18]: a Out[18]: [1, 2, 3, 4, 5] In [25]: (np.array(a) * 5).mean() Out[25]: 15.0 In [26]: labels = [a, b, c, d, e] print labels print a [a, b, c, d, e] [1, 2, 3, 4, 5]http://nbviewer.ipython.org/3904875/ Page 1 of 163
2. 2. 26/01/2013 08:36 In [27]: import pandas as pd In [29]: s = pd.Series(a, labels) s Out[29]: a 1 b 2 c 3 d 4 e 5 In [36]: arr[arr > 3] Out[36]: array([4, 5]) In [53]: dct = {a : 1, b : 4, c: 7} s = pd.Series(dct, index=[c, a, b, d]) s[-s.isnull()] Out[53]: c 7 a 1 b 4 In [57]: s.index Out[57]: Index([c, a, b, d], dtype=object) In [59]: pd.set_printoptions(notebook_repr_html=False) In [62]: data = {one: [1, 2, 3, 4], two: [foo, bar, baz, qux]} df = pd.DataFrame(data, index=[a, b, c, d]) In [65]: df[three] = df[one] * 4 In [68]: df.values[2] Out[68]: array([3, baz, 12], dtype=object) In [75]: df Out[75]: one two three a 1 foo 4 b 2 bar 8 c 3 baz 12 d 4 qux 16 In [79]: s1 = df[one] s2 = df[one][:3] s2 Out[79]: a 1 b 2 c 3 Name: one In [81]: pd.DataFrame({1: s1, 2: s2}, index=[b, a, d])http://nbviewer.ipython.org/3904875/ Page 2 of 163
3. 3. 26/01/2013 08:36 Out[81]: 1 2 b 2 2 a 1 1 d 4 NaN In [90]: (s1 + s2).fillna(method=ffill) Out[90]: a 2 b 4 c 6 d 6 Name: one In [91]: import numpy as np import pandas as pd import simplejson as json db_path = foods-2011-10-03.json db = json.load(open(db_path)) In [96]: nts = pd.DataFrame(db[0][nutrients]) In [110]: nts Out[110]: description group units value 0 Protein Composition g 25.180 1 Total lipid (fat) Composition g 29.200 2 Carbohydrate, by difference Composition g 3.060 3 Ash Other g 3.280 4 Energy Energy kcal 376.000 5 Water Composition g 39.280 6 Energy Energy kJ 1573.000 7 Fiber, total dietary Composition g 0.000 8 Calcium, Ca Elements mg 673.000 9 Iron, Fe Elements mg 0.640 10 Magnesium, Mg Elements mg 22.000 11 Phosphorus, P Elements mg 490.000 12 Potassium, K Elements mg 93.000 13 Sodium, Na Elements mg 690.000 14 Zinc, Zn Elements mg 2.940 15 Copper, Cu Elements mg 0.024 16 Manganese, Mn Elements mg 0.021 17 Selenium, Se Elements mcg 14.500 18 Vitamin A, IU Vitamins IU 1054.000 19 Retinol Vitamins mcg 262.000 20 Vitamin A, RAE Vitamins mcg_RAE 271.000 21 Vitamin C, total ascorbic acid Vitamins mg 0.000 22 Thiamin Vitamins mg 0.031 23 Riboflavin Vitamins mg 0.450 24 Niacin Vitamins mg 0.180 25 Pantothenic acid Vitamins mg 0.190 26 Vitamin B-6 Vitamins mg 0.074 27 Folate, total Vitamins mcg 18.000 28 Vitamin B-12 Vitamins mcg 0.270 29 Folic acid Vitamins mcg 0.000 30 Folate, food Vitamins mcg 18.000 31 Folate, DFE Vitamins mcg_DFE 18.000 32 Cholesterol Other mg 93.000 33 Fatty acids, total saturated Other g 18.584 34 Fatty acids, total monounsaturated Other g 8.275http://nbviewer.ipython.org/3904875/ Page 3 of 163
4. 4. 26/01/2013 08:36 35 Fatty acids, total polyunsaturated Other g 0.830 36 Tryptophan Amino Acids g 0.324 37 Threonine Amino Acids g 0.896 38 Isoleucine Amino Acids g 1.563 39 Leucine Amino Acids g 2.412 40 Lysine Amino Acids g 2.095 41 Methionine Amino Acids g 0.659 42 Cystine Amino Acids g 0.126 43 Phenylalanine Amino Acids g 1.326 44 Tyrosine Amino Acids g 1.216 45 Valine Amino Acids g 1.682 46 Arginine Amino Acids g 0.952 47 Histidine Amino Acids g 0.884 48 Alanine Amino Acids g 0.711 49 Aspartic acid Amino Acids g 1.618 50 Glutamic acid Amino Acids g 6.160 51 Glycine Amino Acids g 0.439 52 Proline Amino Acids g 2.838 53 Serine Amino Acids g 1.472 54 Protein Composition g 25.180 55 Total lipid (fat) Composition g 29.200 56 Carbohydrate, by difference Composition g 3.060 57 Ash Other g 3.280 58 Energy Energy kcal 376.000 59 Water Composition g 39.280 60 Energy Energy kJ 1573.000 61 Fiber, total dietary Composition g 0.000 62 Calcium, Ca Elements mg 673.000 63 Iron, Fe Elements mg 0.640 64 Magnesium, Mg Elements mg 22.000 65 Phosphorus, P Elements mg 490.000 66 Potassium, K Elements mg 93.000 67 Sodium, Na Elements mg 690.000 68 Zinc, Zn Elements mg 2.940 69 Copper, Cu Elements mg 0.024 70 Manganese, Mn Elements mg 0.021 71 Selenium, Se Elements mcg 14.500 72 Vitamin A, IU Vitamins IU 1054.000 73 Retinol Vitamins mcg 262.000 74 Vitamin A, RAE Vitamins mcg_RAE 271.000 75 Vitamin C, total ascorbic acid Vitamins mg 0.000 76 Thiamin Vitamins mg 0.031 77 Riboflavin Vitamins mg 0.450 78 Niacin Vitamins mg 0.180 79 Pantothenic acid Vitamins mg 0.190 80 Vitamin B-6 Vitamins mg 0.074 81 Folate, total Vitamins mcg 18.000 82 Vitamin B-12 Vitamins mcg 0.270 83 Folic acid Vitamins mcg 0.000 84 Folate, food Vitamins mcg 18.000 85 Folate, DFE Vitamins mcg_DFE 18.000 86 Tryptophan Amino Acids g 0.324 87 Threonine Amino Acids g 0.896 88 Isoleucine Amino Acids g 1.563 89 Leucine Amino Acids g 2.412 90 Lysine Amino Acids g 2.095 91 Methionine Amino Acids g 0.659 92 Cystine Amino Acids g 0.126 93 Phenylalanine Amino Acids g 1.326 94 Tyrosine Amino Acids g 1.216 95 Valine Amino Acids g 1.682 96 Arginine Amino Acids g 0.952 97 Histidine Amino Acids g 0.884http://nbviewer.ipython.org/3904875/ Page 4 of 163
5. 5. 26/01/2013 08:36 98 Alanine Amino Acids g 0.711 99 Aspartic acid Amino Acids g 1.618 100 Glutamic acid Amino Acids g 6.160 101 Glycine Amino Acids g 0.439 102 Proline Amino Acids g 2.838 103 Serine Amino Acids g 1.472 104 Cholesterol Other mg 93.000 105 Fatty acids, total saturated Other g 18.584 106 Fatty acids, total monounsaturated Other g 8.275 107 Fatty acids, total polyunsaturated Other g 0.830 108 Protein Composition g 25.180 109 Total lipid (fat) Composition g 29.200 110 Carbohydrate, by difference Composition g 3.060 111 Ash Other g 3.280 112 Energy Energy kcal 376.000 113 Water Composition g 39.280 114 Energy Energy kJ 1573.000 115 Fiber, total dietary Composition g 0.000 116 Calcium, Ca Elements mg 673.000 117 Iron, Fe Elements mg 0.640 118 Magnesium, Mg Elements mg 22.000 119 Phosphorus, P Elements mg 490.000 120 Potassium, K Elements mg 93.000 121 Sodium, Na Elements mg 690.000 122 Zinc, Zn Elements mg 2.940 123 Copper, Cu Elements mg 0.024 124 Manganese, Mn Elements mg 0.021 125 Selenium, Se Elements mcg 14.500 126 Vitamin A, IU Vitamins IU 1054.000 127 Retinol Vitamins mcg 262.000 128 Vitamin A, RAE Vitamins mcg_RAE 271.000 129 Vitamin C, total ascorbic acid Vitamins mg 0.000 130 Thiamin Vitamins mg 0.031 131 Riboflavin Vitamins mg 0.450 132 Niacin Vitamins mg 0.180 133 Pantothenic acid Vitamins mg 0.190 134 Vitamin B-6 Vitamins mg 0.074 135 Folate, total Vitamins mcg 18.000 136 Vitamin B-12 Vitamins mcg 0.270 137 Folic acid Vitamins mcg 0.000 138 Folate, food Vitamins mcg 18.000 139 Folate, DFE Vitamins mcg_DFE 18.000 140 Tryptophan Amino Acids g 0.324 141 Threonine Amino Acids g 0.896 142 Isoleucine Amino Acids g 1.563 143 Leucine Amino Acids g 2.412 144 Lysine Amino Acids g 2.095 145 Methionine Amino Acids g 0.659 146 Cystine Amino Acids g 0.126 147 Phenylalanine Amino Acids g 1.326 148 Tyrosine Amino Acids g 1.216 149 Valine Amino Acids g 1.682 150 Arginine Amino Acids g 0.952 151 Histidine Amino Acids g 0.884 152 Alanine Amino Acids g 0.711 153 Aspartic acid Amino Acids g 1.618 154 Glutamic acid Amino Acids g 6.160 155 Glycine Amino Acids g 0.439 156 Proline Amino Acids g 2.838 157 Serine Amino Acids g 1.472 158 Cholesterol Other mg 93.000 159 Fatty acids, total saturated Other g 18.584 160 Fatty acids, total monounsaturated Other g 8.275http://nbviewer.ipython.org/3904875/ Page 5 of 163
7. 7. 26/01/2013 08:36 48 1179 Sour cream, light Dairy and Egg Products 49 1180 Sour cream, fat free Dairy and Egg Products In [109]: len(db) Out[109]: 6636 In [108]: db[0] Out[108]: {description: Cheese, caraway, group: Dairy and Egg Products, id: 1008, manufacturer: , nutrients: [{description: Protein, group: Composition, units: g, value: 25.18}, {description: Total lipid (fat), group: Composition, units: g, value: 29.2}, {description: Carbohydrate, by difference, group: Composition, units: g, value: 3.06}, {description: Ash, group: Other, units: g, value: 3.28}, {description: Energy, group: Energy, units: kcal, value: 376.0}, {description: Water, group: Composition, units: g, value: 39.28}, {description: Energy, group: Energy, units: kJ, value: 1573.0}, {description: Fiber, total dietary, group: Composition, units: g, value: 0.0}, {description: Calcium, Ca, group: Elements, units: mg, value: 673.0}, {description: Iron, Fe, group: Elements, units: mg, value: 0.64}, {description: Magnesium, Mg, group: Elements, units: mg, value: 22.0}, {description: Phosphorus, P, group: Elements, units: mg, value: 490.0}, {description: Potassium, K, group: Elements, units: mg, value: 93.0}, {description: Sodium, Na, group: Elements, units: mg,http://nbviewer.ipython.org/3904875/ Page 7 of 163
8. 8. 26/01/2013 08:36 value: 690.0}, {description: Zinc, Zn, group: Elements, units: mg, value: 2.94}, {description: Copper, Cu, group: Elements, units: mg, value: 0.024}, {description: Manganese, Mn, group: Elements, units: mg, value: 0.021}, {description: Selenium, Se, group: Elements, units: mcg, value: 14.5}, {description: Vitamin A, IU, group: Vitamins, units: IU, value: 1054.0}, {description: Retinol, group: Vitamins, units: mcg, value: 262.0}, {description: Vitamin A, RAE, group: Vitamins, units: mcg_RAE, value: 271.0}, {description: Vitamin C, total ascorbic acid, group: Vitamins, units: mg, value: 0.0}, {description: Thiamin, group: Vitamins, units: mg, value: 0.031}, {description: Riboflavin, group: Vitamins, units: mg, value: 0.45}, {description: Niacin, group: Vitamins, units: mg, value: 0.18}, {description: Pantothenic acid, group: Vitamins, units: mg, value: 0.19}, {description: Vitamin B-6, group: Vitamins, units: mg, value: 0.074}, {description: Folate, total, group: Vitamins, units: mcg, value: 18.0}, {description: Vitamin B-12, group: Vitamins, units: mcg, value: 0.27}, {description: Folic acid, group: Vitamins, units: mcg, value: 0.0}, {description: Folate, food,http://nbviewer.ipython.org/3904875/ Page 8 of 163
9. 9. 26/01/2013 08:36 group: Vitamins, units: mcg, value: 18.0}, {description: Folate, DFE, group: Vitamins, units: mcg_DFE, value: 18.0}, {description: Cholesterol, group: Other, units: mg, value: 93.0}, {description: Fatty acids, total saturated, group: Other, units: g, value: 18.584}, {description: Fatty acids, total monounsaturated, group: Other, units: g, value: 8.275}, {description: Fatty acids, total polyunsaturated, group: Other, units: g, value: 0.83}, {description: Tryptophan, group: Amino Acids, units: g, value: 0.324}, {description: Threonine, group: Amino Acids, units: g, value: 0.896}, {description: Isoleucine, group: Amino Acids, units: g, value: 1.563}, {description: Leucine, group: Amino Acids, units: g, value: 2.412}, {description: Lysine, group: Amino Acids, units: g, value: 2.095}, {description: Methionine, group: Amino Acids, units: g, value: 0.659}, {description: Cystine, group: Amino Acids, units: g, value: 0.126}, {description: Phenylalanine, group: Amino Acids, units: g, value: 1.326}, {description: Tyrosine, group: Amino Acids, units: g, value: 1.216}, {description: Valine, group: Amino Acids, units: g, value: 1.682},http://nbviewer.ipython.org/3904875/ Page 9 of 163
10. 10. 26/01/2013 08:36 {description: Arginine, group: Amino Acids, units: g, value: 0.952}, {description: Histidine, group: Amino Acids, units: g, value: 0.884}, {description: Alanine, group: Amino Acids, units: g, value: 0.711}, {description: Aspartic acid, group: Amino Acids, units: g, value: 1.618}, {description: Glutamic acid, group: Amino Acids, units: g, value: 6.16}, {description: Glycine, group: Amino Acids, units: g, value: 0.439}, {description: Proline, group: Amino Acids, units: g, value: 2.838}, {description: Serine, group: Amino Acids, units: g, value: 1.472}, {description: Protein, group: Composition, units: g, value: 25.18}, {description: Total lipid (fat), group: Composition, units: g, value: 29.2}, {description: Carbohydrate, by difference, group: Composition, units: g, value: 3.06}, {description: Ash, group: Other, units: g, value: 3.28}, {description: Energy, group: Energy, units: kcal, value: 376.0}, {description: Water, group: Composition, units: g, value: 39.28}, {description: Energy, group: Energy, units: kJ, value: 1573.0}, {description: Fiber, total dietary, group: Composition, units: g, value: 0.0}, {description: Calcium, Ca, group: Elements, units: mg, value: 673.0}, {description: Iron, Fe,http://nbviewer.ipython.org/3904875/ Page 10 of 163
11. 11. 26/01/2013 08:36 group: Elements, units: mg, value: 0.64}, {description: Magnesium, Mg, group: Elements, units: mg, value: 22.0}, {description: Phosphorus, P, group: Elements, units: mg, value: 490.0}, {description: Potassium, K, group: Elements, units: mg, value: 93.0}, {description: Sodium, Na, group: Elements, units: mg, value: 690.0}, {description: Zinc, Zn, group: Elements, units: mg, value: 2.94}, {description: Copper, Cu, group: Elements, units: mg, value: 0.024}, {description: Manganese, Mn, group: Elements, units: mg, value: 0.021}, {description: Selenium, Se, group: Elements, units: mcg, value: 14.5}, {description: Vitamin A, IU, group: Vitamins, units: IU, value: 1054.0}, {description: Retinol, group: Vitamins, units: mcg, value: 262.0}, {description: Vitamin A, RAE, group: Vitamins, units: mcg_RAE, value: 271.0}, {description: Vitamin C, total ascorbic acid, group: Vitamins, units: mg, value: 0.0}, {description: Thiamin, group: Vitamins, units: mg, value: 0.031}, {description: Riboflavin, group: Vitamins, units: mg, value: 0.45}, {description: Niacin, group: Vitamins, units: mg, value: 0.18}, {description: Pantothenic acid, group: Vitamins, units: mg,http://nbviewer.ipython.org/3904875/ Page 11 of 163
12. 12. 26/01/2013 08:36 value: 0.19}, {description: Vitamin B-6, group: Vitamins, units: mg, value: 0.074}, {description: Folate, total, group: Vitamins, units: mcg, value: 18.0}, {description: Vitamin B-12, group: Vitamins, units: mcg, value: 0.27}, {description: Folic acid, group: Vitamins, units: mcg, value: 0.0}, {description: Folate, food, group: Vitamins, units: mcg, value: 18.0}, {description: Folate, DFE, group: Vitamins, units: mcg_DFE, value: 18.0}, {description: Tryptophan, group: Amino Acids, units: g, value: 0.324}, {description: Threonine, group: Amino Acids, units: g, value: 0.896}, {description: Isoleucine, group: Amino Acids, units: g, value: 1.563}, {description: Leucine, group: Amino Acids, units: g, value: 2.412}, {description: Lysine, group: Amino Acids, units: g, value: 2.095}, {description: Methionine, group: Amino Acids, units: g, value: 0.659}, {description: Cystine, group: Amino Acids, units: g, value: 0.126}, {description: Phenylalanine, group: Amino Acids, units: g, value: 1.326}, {description: Tyrosine, group: Amino Acids, units: g, value: 1.216}, {description: Valine, group: Amino Acids,http://nbviewer.ipython.org/3904875/ Page 12 of 163
13. 13. 26/01/2013 08:36 units: g, value: 1.682}, {description: Arginine, group: Amino Acids, units: g, value: 0.952}, {description: Histidine, group: Amino Acids, units: g, value: 0.884}, {description: Alanine, group: Amino Acids, units: g, value: 0.711}, {description: Aspartic acid, group: Amino Acids, units: g, value: 1.618}, {description: Glutamic acid, group: Amino Acids, units: g, value: 6.16}, {description: Glycine, group: Amino Acids, units: g, value: 0.439}, {description: Proline, group: Amino Acids, units: g, value: 2.838}, {description: Serine, group: Amino Acids, units: g, value: 1.472}, {description: Cholesterol, group: Other, units: mg, value: 93.0}, {description: Fatty acids, total saturated, group: Other, units: g, value: 18.584}, {description: Fatty acids, total monounsaturated, group: Other, units: g, value: 8.275}, {description: Fatty acids, total polyunsaturated, group: Other, units: g, value: 0.83}, {description: Protein, group: Composition, units: g, value: 25.18}, {description: Total lipid (fat), group: Composition, units: g, value: 29.2}, {description: Carbohydrate, by difference, group: Composition, units: g, value: 3.06}, {description: Ash, group: Other, units: g, value: 3.28},http://nbviewer.ipython.org/3904875/ Page 13 of 163
14. 14. 26/01/2013 08:36 {description: Energy, group: Energy, units: kcal, value: 376.0}, {description: Water, group: Composition, units: g, value: 39.28}, {description: Energy, group: Energy, units: kJ, value: 1573.0}, {description: Fiber, total dietary, group: Composition, units: g, value: 0.0}, {description: Calcium, Ca, group: Elements, units: mg, value: 673.0}, {description: Iron, Fe, group: Elements, units: mg, value: 0.64}, {description: Magnesium, Mg, group: Elements, units: mg, value: 22.0}, {description: Phosphorus, P, group: Elements, units: mg, value: 490.0}, {description: Potassium, K, group: Elements, units: mg, value: 93.0}, {description: Sodium, Na, group: Elements, units: mg, value: 690.0}, {description: Zinc, Zn, group: Elements, units: mg, value: 2.94}, {description: Copper, Cu, group: Elements, units: mg, value: 0.024}, {description: Manganese, Mn, group: Elements, units: mg, value: 0.021}, {description: Selenium, Se, group: Elements, units: mcg, value: 14.5}, {description: Vitamin A, IU, group: Vitamins, units: IU, value: 1054.0}, {description: Retinol, group: Vitamins, units: mcg, value: 262.0}, {description: Vitamin A, RAE, group: Vitamins,http://nbviewer.ipython.org/3904875/ Page 14 of 163
15. 15. 26/01/2013 08:36 units: mcg_RAE, value: 271.0}, {description: Vitamin C, total ascorbic acid, group: Vitamins, units: mg, value: 0.0}, {description: Thiamin, group: Vitamins, units: mg, value: 0.031}, {description: Riboflavin, group: Vitamins, units: mg, value: 0.45}, {description: Niacin, group: Vitamins, units: mg, value: 0.18}, {description: Pantothenic acid, group: Vitamins, units: mg, value: 0.19}, {description: Vitamin B-6, group: Vitamins, units: mg, value: 0.074}, {description: Folate, total, group: Vitamins, units: mcg, value: 18.0}, {description: Vitamin B-12, group: Vitamins, units: mcg, value: 0.27}, {description: Folic acid, group: Vitamins, units: mcg, value: 0.0}, {description: Folate, food, group: Vitamins, units: mcg, value: 18.0}, {description: Folate, DFE, group: Vitamins, units: mcg_DFE, value: 18.0}, {description: Tryptophan, group: Amino Acids, units: g, value: 0.324}, {description: Threonine, group: Amino Acids, units: g, value: 0.896}, {description: Isoleucine, group: Amino Acids, units: g, value: 1.563}, {description: Leucine, group: Amino Acids, units: g, value: 2.412}, {description: Lysine, group: Amino Acids, units: g, value: 2.095},http://nbviewer.ipython.org/3904875/ Page 15 of 163
16. 16. 26/01/2013 08:36 {description: Methionine, group: Amino Acids, units: g, value: 0.659}, {description: Cystine, group: Amino Acids, units: g, value: 0.126}, {description: Phenylalanine, group: Amino Acids, units: g, value: 1.326}, {description: Tyrosine, group: Amino Acids, units: g, value: 1.216}, {description: Valine, group: Amino Acids, units: g, value: 1.682}, {description: Arginine, group: Amino Acids, units: g, value: 0.952}, {description: Histidine, group: Amino Acids, units: g, value: 0.884}, {description: Alanine, group: Amino Acids, units: g, value: 0.711}, {description: Aspartic acid, group: Amino Acids, units: g, value: 1.618}, {description: Glutamic acid, group: Amino Acids, units: g, value: 6.16}, {description: Glycine, group: Amino Acids, units: g, value: 0.439}, {description: Proline, group: Amino Acids, units: g, value: 2.838}, {description: Serine, group: Amino Acids, units: g, value: 1.472}, {description: Cholesterol, group: Other, units: mg, value: 93.0}, {description: Fatty acids, total saturated, group: Other, units: g, value: 18.584}, {description: Fatty acids, total monounsaturated, group: Other, units: g,http://nbviewer.ipython.org/3904875/ Page 16 of 163
17. 17. 26/01/2013 08:36 value: 8.275}, {description: Fatty acids, total polyunsaturated, group: Other, units: g, value: 0.83}], portions: [{amount: 1, grams: 28.35, unit: oz}], tags: []} In [107]: nts Out[107]: description group units value 0 Protein Composition g 25.180 1 Total lipid (fat) Composition g 29.200 2 Carbohydrate, by difference Composition g 3.060 3 Ash Other g 3.280 4 Energy Energy kcal 376.000 5 Water Composition g 39.280 6 Energy Energy kJ 1573.000 7 Fiber, total dietary Composition g 0.000 8 Calcium, Ca Elements mg 673.000 9 Iron, Fe Elements mg 0.640 10 Magnesium, Mg Elements mg 22.000 11 Phosphorus, P Elements mg 490.000 12 Potassium, K Elements mg 93.000 13 Sodium, Na Elements mg 690.000 14 Zinc, Zn Elements mg 2.940 15 Copper, Cu Elements mg 0.024 16 Manganese, Mn Elements mg 0.021 17 Selenium, Se Elements mcg 14.500 18 Vitamin A, IU Vitamins IU 1054.000 19 Retinol Vitamins mcg 262.000 20 Vitamin A, RAE Vitamins mcg_RAE 271.000 21 Vitamin C, total ascorbic acid Vitamins mg 0.000 22 Thiamin Vitamins mg 0.031 23 Riboflavin Vitamins mg 0.450 24 Niacin Vitamins mg 0.180 25 Pantothenic acid Vitamins mg 0.190 26 Vitamin B-6 Vitamins mg 0.074 27 Folate, total Vitamins mcg 18.000 28 Vitamin B-12 Vitamins mcg 0.270 29 Folic acid Vitamins mcg 0.000 30 Folate, food Vitamins mcg 18.000 31 Folate, DFE Vitamins mcg_DFE 18.000 32 Cholesterol Other mg 93.000 33 Fatty acids, total saturated Other g 18.584 34 Fatty acids, total monounsaturated Other g 8.275 35 Fatty acids, total polyunsaturated Other g 0.830 36 Tryptophan Amino Acids g 0.324 37 Threonine Amino Acids g 0.896 38 Isoleucine Amino Acids g 1.563 39 Leucine Amino Acids g 2.412 40 Lysine Amino Acids g 2.095 41 Methionine Amino Acids g 0.659 42 Cystine Amino Acids g 0.126 43 Phenylalanine Amino Acids g 1.326 44 Tyrosine Amino Acids g 1.216 45 Valine Amino Acids g 1.682 46 Arginine Amino Acids g 0.952 47 Histidine Amino Acids g 0.884 48 Alanine Amino Acids g 0.711 49 Aspartic acid Amino Acids g 1.618 50 Glutamic acid Amino Acids g 6.160http://nbviewer.ipython.org/3904875/ Page 17 of 163
18. 18. 26/01/2013 08:36 51 Glycine Amino Acids g 0.439 52 Proline Amino Acids g 2.838 53 Serine Amino Acids g 1.472 54 Protein Composition g 25.180 55 Total lipid (fat) Composition g 29.200 56 Carbohydrate, by difference Composition g 3.060 57 Ash Other g 3.280 58 Energy Energy kcal 376.000 59 Water Composition g 39.280 60 Energy Energy kJ 1573.000 61 Fiber, total dietary Composition g 0.000 62 Calcium, Ca Elements mg 673.000 63 Iron, Fe Elements mg 0.640 64 Magnesium, Mg Elements mg 22.000 65 Phosphorus, P Elements mg 490.000 66 Potassium, K Elements mg 93.000 67 Sodium, Na Elements mg 690.000 68 Zinc, Zn Elements mg 2.940 69 Copper, Cu Elements mg 0.024 70 Manganese, Mn Elements mg 0.021 71 Selenium, Se Elements mcg 14.500 72 Vitamin A, IU Vitamins IU 1054.000 73 Retinol Vitamins mcg 262.000 74 Vitamin A, RAE Vitamins mcg_RAE 271.000 75 Vitamin C, total ascorbic acid Vitamins mg 0.000 76 Thiamin Vitamins mg 0.031 77 Riboflavin Vitamins mg 0.450 78 Niacin Vitamins mg 0.180 79 Pantothenic acid Vitamins mg 0.190 80 Vitamin B-6 Vitamins mg 0.074 81 Folate, total Vitamins mcg 18.000 82 Vitamin B-12 Vitamins mcg 0.270 83 Folic acid Vitamins mcg 0.000 84 Folate, food Vitamins mcg 18.000 85 Folate, DFE Vitamins mcg_DFE 18.000 86 Tryptophan Amino Acids g 0.324 87 Threonine Amino Acids g 0.896 88 Isoleucine Amino Acids g 1.563 89 Leucine Amino Acids g 2.412 90 Lysine Amino Acids g 2.095 91 Methionine Amino Acids g 0.659 92 Cystine Amino Acids g 0.126 93 Phenylalanine Amino Acids g 1.326 94 Tyrosine Amino Acids g 1.216 95 Valine Amino Acids g 1.682 96 Arginine Amino Acids g 0.952 97 Histidine Amino Acids g 0.884 98 Alanine Amino Acids g 0.711 99 Aspartic acid Amino Acids g 1.618 100 Glutamic acid Amino Acids g 6.160 101 Glycine Amino Acids g 0.439 102 Proline Amino Acids g 2.838 103 Serine Amino Acids g 1.472 104 Cholesterol Other mg 93.000 105 Fatty acids, total saturated Other g 18.584 106 Fatty acids, total monounsaturated Other g 8.275 107 Fatty acids, total polyunsaturated Other g 0.830 108 Protein Composition g 25.180 109 Total lipid (fat) Composition g 29.200 110 Carbohydrate, by difference Composition g 3.060 111 Ash Other g 3.280 112 Energy Energy kcal 376.000 113 Water Composition g 39.280http://nbviewer.ipython.org/3904875/ Page 18 of 163
19. 19. 26/01/2013 08:36 114 Energy Energy kJ 1573.000 115 Fiber, total dietary Composition g 0.000 116 Calcium, Ca Elements mg 673.000 117 Iron, Fe Elements mg 0.640 118 Magnesium, Mg Elements mg 22.000 119 Phosphorus, P Elements mg 490.000 120 Potassium, K Elements mg 93.000 121 Sodium, Na Elements mg 690.000 122 Zinc, Zn Elements mg 2.940 123 Copper, Cu Elements mg 0.024 124 Manganese, Mn Elements mg 0.021 125 Selenium, Se Elements mcg 14.500 126 Vitamin A, IU Vitamins IU 1054.000 127 Retinol Vitamins mcg 262.000 128 Vitamin A, RAE Vitamins mcg_RAE 271.000 129 Vitamin C, total ascorbic acid Vitamins mg 0.000 130 Thiamin Vitamins mg 0.031 131 Riboflavin Vitamins mg 0.450 132 Niacin Vitamins mg 0.180 133 Pantothenic acid Vitamins mg 0.190 134 Vitamin B-6 Vitamins mg 0.074 135 Folate, total Vitamins mcg 18.000 136 Vitamin B-12 Vitamins mcg 0.270 137 Folic acid Vitamins mcg 0.000 138 Folate, food Vitamins mcg 18.000 139 Folate, DFE Vitamins mcg_DFE 18.000 140 Tryptophan Amino Acids g 0.324 141 Threonine Amino Acids g 0.896 142 Isoleucine Amino Acids g 1.563 143 Leucine Amino Acids g 2.412 144 Lysine Amino Acids g 2.095 145 Methionine Amino Acids g 0.659 146 Cystine Amino Acids g 0.126 147 Phenylalanine Amino Acids g 1.326 148 Tyrosine Amino Acids g 1.216 149 Valine Amino Acids g 1.682 150 Arginine Amino Acids g 0.952 151 Histidine Amino Acids g 0.884 152 Alanine Amino Acids g 0.711 153 Aspartic acid Amino Acids g 1.618 154 Glutamic acid Amino Acids g 6.160 155 Glycine Amino Acids g 0.439 156 Proline Amino Acids g 2.838 157 Serine Amino Acids g 1.472 158 Cholesterol Other mg 93.000 159 Fatty acids, total saturated Other g 18.584 160 Fatty acids, total monounsaturated Other g 8.275 161 Fatty acids, total polyunsaturated Other g 0.830 In [123]: info.group.value_counts().plot(kind=bar) Out[123]: <matplotlib.axes.AxesSubplot at 0x10bc66e50>http://nbviewer.ipython.org/3904875/ Page 19 of 163
20. 20. 26/01/2013 08:36 In [126]: info.head() Out[126]: id description group 0 1008 Cheese, caraway Dairy and Egg Products 1 1009 Cheese, cheddar Dairy and Egg Products 2 1018 Cheese, edam Dairy and Egg Products 3 1019 Cheese, feta Dairy and Egg Products 4 1028 Cheese, mozzarella, part skim milk Dairy and Egg Products In [125]: nts.head() Out[125]: description group units value 0 Protein Composition g 25.18 1 Total lipid (fat) Composition g 29.20 2 Carbohydrate, by difference Composition g 3.06 3 Ash Other g 3.28 4 Energy Energy kcal 376.00 In [124]: info Out[124]: <class pandas.core.frame.DataFrame> Int64Index: 6636 entries, 0 to 6635 Data columns: id 6636 non-null values description 6636 non-null values group 6636 non-null values dtypes: int64(1), object(2) In [128]: # info.columns = [id, fname, fgroup] mapping = {description: fname, group : fgroup} info = info.rename(columns=mapping) info.head() Out[128]: id fname fgroup 0 1008 Cheese, caraway Dairy and Egg Products 1 1009 Cheese, cheddar Dairy and Egg Products 2 1018 Cheese, edam Dairy and Egg Products 3 1019 Cheese, feta Dairy and Egg Products 4 1028 Cheese, mozzarella, part skim milk Dairy and Egg Products In [129]: mapping = {description: nname, group : ngroup} nts = nts.rename(columns=mapping)http://nbviewer.ipython.org/3904875/ Page 20 of 163
21. 21. 26/01/2013 08:36 nts Out[129]: nname ngroup units value 0 Protein Composition g 25.180 1 Total lipid (fat) Composition g 29.200 2 Carbohydrate, by difference Composition g 3.060 3 Ash Other g 3.280 4 Energy Energy kcal 376.000 5 Water Composition g 39.280 6 Energy Energy kJ 1573.000 7 Fiber, total dietary Composition g 0.000 8 Calcium, Ca Elements mg 673.000 9 Iron, Fe Elements mg 0.640 10 Magnesium, Mg Elements mg 22.000 11 Phosphorus, P Elements mg 490.000 12 Potassium, K Elements mg 93.000 13 Sodium, Na Elements mg 690.000 14 Zinc, Zn Elements mg 2.940 15 Copper, Cu Elements mg 0.024 16 Manganese, Mn Elements mg 0.021 17 Selenium, Se Elements mcg 14.500 18 Vitamin A, IU Vitamins IU 1054.000 19 Retinol Vitamins mcg 262.000 20 Vitamin A, RAE Vitamins mcg_RAE 271.000 21 Vitamin C, total ascorbic acid Vitamins mg 0.000 22 Thiamin Vitamins mg 0.031 23 Riboflavin Vitamins mg 0.450 24 Niacin Vitamins mg 0.180 25 Pantothenic acid Vitamins mg 0.190 26 Vitamin B-6 Vitamins mg 0.074 27 Folate, total Vitamins mcg 18.000 28 Vitamin B-12 Vitamins mcg 0.270 29 Folic acid Vitamins mcg 0.000 30 Folate, food Vitamins mcg 18.000 31 Folate, DFE Vitamins mcg_DFE 18.000 32 Cholesterol Other mg 93.000 33 Fatty acids, total saturated Other g 18.584 34 Fatty acids, total monounsaturated Other g 8.275 35 Fatty acids, total polyunsaturated Other g 0.830 36 Tryptophan Amino Acids g 0.324 37 Threonine Amino Acids g 0.896 38 Isoleucine Amino Acids g 1.563 39 Leucine Amino Acids g 2.412 40 Lysine Amino Acids g 2.095 41 Methionine Amino Acids g 0.659 42 Cystine Amino Acids g 0.126 43 Phenylalanine Amino Acids g 1.326 44 Tyrosine Amino Acids g 1.216 45 Valine Amino Acids g 1.682 46 Arginine Amino Acids g 0.952 47 Histidine Amino Acids g 0.884 48 Alanine Amino Acids g 0.711 49 Aspartic acid Amino Acids g 1.618 50 Glutamic acid Amino Acids g 6.160 51 Glycine Amino Acids g 0.439 52 Proline Amino Acids g 2.838 53 Serine Amino Acids g 1.472 54 Protein Composition g 25.180 55 Total lipid (fat) Composition g 29.200 56 Carbohydrate, by difference Composition g 3.060 57 Ash Other g 3.280 58 Energy Energy kcal 376.000 59 Water Composition g 39.280http://nbviewer.ipython.org/3904875/ Page 21 of 163
22. 22. 26/01/2013 08:36 60 Energy Energy kJ 1573.000 61 Fiber, total dietary Composition g 0.000 62 Calcium, Ca Elements mg 673.000 63 Iron, Fe Elements mg 0.640 64 Magnesium, Mg Elements mg 22.000 65 Phosphorus, P Elements mg 490.000 66 Potassium, K Elements mg 93.000 67 Sodium, Na Elements mg 690.000 68 Zinc, Zn Elements mg 2.940 69 Copper, Cu Elements mg 0.024 70 Manganese, Mn Elements mg 0.021 71 Selenium, Se Elements mcg 14.500 72 Vitamin A, IU Vitamins IU 1054.000 73 Retinol Vitamins mcg 262.000 74 Vitamin A, RAE Vitamins mcg_RAE 271.000 75 Vitamin C, total ascorbic acid Vitamins mg 0.000 76 Thiamin Vitamins mg 0.031 77 Riboflavin Vitamins mg 0.450 78 Niacin Vitamins mg 0.180 79 Pantothenic acid Vitamins mg 0.190 80 Vitamin B-6 Vitamins mg 0.074 81 Folate, total Vitamins mcg 18.000 82 Vitamin B-12 Vitamins mcg 0.270 83 Folic acid Vitamins mcg 0.000 84 Folate, food Vitamins mcg 18.000 85 Folate, DFE Vitamins mcg_DFE 18.000 86 Tryptophan Amino Acids g 0.324 87 Threonine Amino Acids g 0.896 88 Isoleucine Amino Acids g 1.563 89 Leucine Amino Acids g 2.412 90 Lysine Amino Acids g 2.095 91 Methionine Amino Acids g 0.659 92 Cystine Amino Acids g 0.126 93 Phenylalanine Amino Acids g 1.326 94 Tyrosine Amino Acids g 1.216 95 Valine Amino Acids g 1.682 96 Arginine Amino Acids g 0.952 97 Histidine Amino Acids g 0.884 98 Alanine Amino Acids g 0.711 99 Aspartic acid Amino Acids g 1.618 100 Glutamic acid Amino Acids g 6.160 101 Glycine Amino Acids g 0.439 102 Proline Amino Acids g 2.838 103 Serine Amino Acids g 1.472 104 Cholesterol Other mg 93.000 105 Fatty acids, total saturated Other g 18.584 106 Fatty acids, total monounsaturated Other g 8.275 107 Fatty acids, total polyunsaturated Other g 0.830 108 Protein Composition g 25.180 109 Total lipid (fat) Composition g 29.200 110 Carbohydrate, by difference Composition g 3.060 111 Ash Other g 3.280 112 Energy Energy kcal 376.000 113 Water Composition g 39.280 114 Energy Energy kJ 1573.000 115 Fiber, total dietary Composition g 0.000 116 Calcium, Ca Elements mg 673.000 117 Iron, Fe Elements mg 0.640 118 Magnesium, Mg Elements mg 22.000 119 Phosphorus, P Elements mg 490.000 120 Potassium, K Elements mg 93.000 121 Sodium, Na Elements mg 690.000 122 Zinc, Zn Elements mg 2.940http://nbviewer.ipython.org/3904875/ Page 22 of 163
23. 23. 26/01/2013 08:36 123 Copper, Cu Elements mg 0.024 124 Manganese, Mn Elements mg 0.021 125 Selenium, Se Elements mcg 14.500 126 Vitamin A, IU Vitamins IU 1054.000 127 Retinol Vitamins mcg 262.000 128 Vitamin A, RAE Vitamins mcg_RAE 271.000 129 Vitamin C, total ascorbic acid Vitamins mg 0.000 130 Thiamin Vitamins mg 0.031 131 Riboflavin Vitamins mg 0.450 132 Niacin Vitamins mg 0.180 133 Pantothenic acid Vitamins mg 0.190 134 Vitamin B-6 Vitamins mg 0.074 135 Folate, total Vitamins mcg 18.000 136 Vitamin B-12 Vitamins mcg 0.270 137 Folic acid Vitamins mcg 0.000 138 Folate, food Vitamins mcg 18.000 139 Folate, DFE Vitamins mcg_DFE 18.000 140 Tryptophan Amino Acids g 0.324 141 Threonine Amino Acids g 0.896 142 Isoleucine Amino Acids g 1.563 143 Leucine Amino Acids g 2.412 144 Lysine Amino Acids g 2.095 145 Methionine Amino Acids g 0.659 146 Cystine Amino Acids g 0.126 147 Phenylalanine Amino Acids g 1.326 148 Tyrosine Amino Acids g 1.216 149 Valine Amino Acids g 1.682 150 Arginine Amino Acids g 0.952 151 Histidine Amino Acids g 0.884 152 Alanine Amino Acids g 0.711 153 Aspartic acid Amino Acids g 1.618 154 Glutamic acid Amino Acids g 6.160 155 Glycine Amino Acids g 0.439 156 Proline Amino Acids g 2.838 157 Serine Amino Acids g 1.472 158 Cholesterol Other mg 93.000 159 Fatty acids, total saturated Other g 18.584 160 Fatty acids, total monounsaturated Other g 8.275 161 Fatty acids, total polyunsaturated Other g 0.830 In [133]: db[0] Out[133]: {description: Cheese, caraway, group: Dairy and Egg Products, id: 1008, manufacturer: , nutrients: [{description: Protein, group: Composition, units: g, value: 25.18}, {description: Total lipid (fat), group: Composition, units: g, value: 29.2}, {description: Carbohydrate, by difference, group: Composition, units: g, value: 3.06}, {description: Ash, group: Other, units: g, value: 3.28}, {description: Energy, group: Energy, units: kcal,http://nbviewer.ipython.org/3904875/ Page 23 of 163
24. 24. 26/01/2013 08:36 value: 376.0}, {description: Water, group: Composition, units: g, value: 39.28}, {description: Energy, group: Energy, units: kJ, value: 1573.0}, {description: Fiber, total dietary, group: Composition, units: g, value: 0.0}, {description: Calcium, Ca, group: Elements, units: mg, value: 673.0}, {description: Iron, Fe, group: Elements, units: mg, value: 0.64}, {description: Magnesium, Mg, group: Elements, units: mg, value: 22.0}, {description: Phosphorus, P, group: Elements, units: mg, value: 490.0}, {description: Potassium, K, group: Elements, units: mg, value: 93.0}, {description: Sodium, Na, group: Elements, units: mg, value: 690.0}, {description: Zinc, Zn, group: Elements, units: mg, value: 2.94}, {description: Copper, Cu, group: Elements, units: mg, value: 0.024}, {description: Manganese, Mn, group: Elements, units: mg, value: 0.021}, {description: Selenium, Se, group: Elements, units: mcg, value: 14.5}, {description: Vitamin A, IU, group: Vitamins, units: IU, value: 1054.0}, {description: Retinol, group: Vitamins, units: mcg, value: 262.0}, {description: Vitamin A, RAE, group: Vitamins, units: mcg_RAE, value: 271.0}, {description: Vitamin C, total ascorbic acid,http://nbviewer.ipython.org/3904875/ Page 24 of 163