Assignment 4

In this assignment, you'll combine the assignment 3 data set with nutrition data from the USDA Food Composition Databases. The CSV file fresh.csv contains the fresh fruits and vegetables data you extracted in assignment 3.

The USDA Food Composition Databases have a documented web API that returns data in JSON format . You need a key in order to use the API. Only 1000 requests are allowed per hour, so it would be a good idea to use caching.

Sign up for an API key here. The key will work with any Data.gov API. You may need the key again later in the quarter, so make sure you save it.

These modules may be useful:

Exercise 1.1. Read the search request documentation, then write a function called ndb_search() that makes a search request. The function should accept the search term as an argument. The function should return the search result items as a list (for 0 items, return an empty list).

Note that the search url is: https://api.nal.usda.gov/ndb/search

As an example, a search for "quail eggs" should return this list:

[{u'ds': u'BL',
  u'group': u'Branded Food Products Database',
  u'name': u'CHAOKOH, QUAIL EGG IN BRINE, UPC: 044738074186',
  u'ndbno': u'45094707',
  u'offset': 0},
 {u'ds': u'BL',
  u'group': u'Branded Food Products Database',
  u'name': u'L&W, QUAIL EGGS, UPC: 024072000256',
  u'ndbno': u'45094890',
  u'offset': 1},
 {u'ds': u'BL',
  u'group': u'Branded Food Products Database',
  u'name': u'BUDDHA, QUAIL EGGS IN BRINE, UPC: 761934535098',
  u'ndbno': u'45099560',
  u'offset': 2},
 {u'ds': u'BL',
  u'group': u'Branded Food Products Database',
  u'name': u'GRAN SABANA, QUAIL EGGS, UPC: 819140010103',
  u'ndbno': u'45169279',
  u'offset': 3},
 {u'ds': u'BL',
  u'group': u'Branded Food Products Database',
  u'name': u"D'ARTAGNAN, QUAIL EGGS, UPC: 736622102630",
  u'ndbno': u'45178254',
  u'offset': 4},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Egg, quail, whole, fresh, raw',
  u'ndbno': u'01140',
  u'offset': 5}]

As usual, make sure you document and test your function.

In [158]:
from urllib2 import Request, urlopen
import pandas as pd
import requests
import json
from urlparse import urlunparse, urlparse
In [159]:
key = "IcqWz29klKjRfiCAGy2AZvLbt5COAgmqpWy7CbAP"
def ndb_search(term):
    temp = []
    dictList = []
    url = "https://api.nal.usda.gov/ndb/search/?format=json&max=500"
    url = url + "&q=" + term + "&api_key=" + key
    req = requests.get(url)
    x = req.json()
    try:
        x['errors']
        return 0  
    except:
         return x['list']['item']
        
    
ndb_search("quail eggs")    
Out[159]:
[{u'ds': u'BL',
  u'group': u'Branded Food Products Database',
  u'name': u'CHAOKOH, QUAIL EGG IN BRINE, UPC: 044738074186',
  u'ndbno': u'45094707',
  u'offset': 0},
 {u'ds': u'BL',
  u'group': u'Branded Food Products Database',
  u'name': u'L&W, QUAIL EGGS, UPC: 024072000256',
  u'ndbno': u'45094890',
  u'offset': 1},
 {u'ds': u'BL',
  u'group': u'Branded Food Products Database',
  u'name': u'BUDDHA, QUAIL EGGS IN BRINE, UPC: 761934535098',
  u'ndbno': u'45099560',
  u'offset': 2},
 {u'ds': u'BL',
  u'group': u'Branded Food Products Database',
  u'name': u'GRAN SABANA, QUAIL EGGS, UPC: 819140010103',
  u'ndbno': u'45169279',
  u'offset': 3},
 {u'ds': u'BL',
  u'group': u'Branded Food Products Database',
  u'name': u"D'ARTAGNAN, QUAIL EGGS, UPC: 736622102630",
  u'ndbno': u'45178254',
  u'offset': 4},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Egg, quail, whole, fresh, raw',
  u'ndbno': u'01140',
  u'offset': 5}]

Exercise 1.2. Use your search function to get NDB numbers for the foods in the fresh.csv file. It's okay if you don't get an NDB number for every food, but try to come up with a strategy that gets most of them. Discuss your strategy in a short paragraph.

Hints:

  • The foods are all raw and unbranded.
  • You can test search terms with the online search page.
  • You can convert the output of ndb_search() to a data frame with pd.DataFrame().
  • The string methods for Python and Pandas are useful here. It's okay if you use simple regular expressions in the Pandas methods, although this exercise can be solved without them.
  • You can merge data frames that have a column in common with pd.merge().
In [160]:
data = pd.read_csv("fresh.csv")
data
Out[160]:
form price_per_lb yield lb_per_cup price_per_cup food type
0 Fresh1 0.333412 0.520000 0.330693 0.212033 watermelon fruit
1 Fresh1 0.535874 0.510000 0.374786 0.393800 cantaloupe fruit
2 Fresh1 1.377962 0.740000 0.407855 0.759471 tangerines fruit
3 Fresh1 2.358808 0.940000 0.319670 0.802171 strawberries fruit
4 Fresh1 1.827416 0.940000 0.363763 0.707176 plums fruit
5 Fresh1 1.035173 0.730000 0.407855 0.578357 oranges fruit
6 Fresh1 6.975811 0.960000 0.319670 2.322874 raspberries fruit
7 Fresh1 2.173590 0.560000 0.341717 1.326342 pomegranate fruit
8 Fresh1 0.627662 0.510000 0.363763 0.447686 pineapple fruit
9 Fresh1 3.040072 0.930000 0.363763 1.189102 apricots fruit
10 Fresh1 0.796656 0.460000 0.374786 0.649077 honeydew fruit
11 Fresh1 1.298012 0.620000 0.308647 0.646174 papaya fruit
12 Fresh1 2.044683 0.760000 0.385809 1.037970 kiwi fruit
13 Fresh1 3.592990 0.920000 0.341717 1.334548 cherries fruit
14 Fresh1 0.566983 0.640000 0.330693 0.292965 bananas fruit
15 Fresh1 1.567515 0.900000 0.242508 0.422373 apples fruit
16 Fresh1 1.591187 0.960000 0.341717 0.566390 peaches fruit
17 Fresh1 1.761148 0.910000 0.319670 0.618667 nectarines fruit
18 Fresh1 1.461575 0.900000 0.363763 0.590740 pears fruit
19 Fresh1 0.897802 0.490000 0.462971 0.848278 grapefruit fruit
20 Fresh1 5.774708 0.960000 0.319670 1.922919 blackberries fruit
21 Fresh1 2.093827 0.960000 0.330693 0.721266 grapes fruit
22 Fresh1 4.734622 0.950000 0.319670 1.593177 blueberries fruit
23 Fresh1 1.377563 0.710000 0.363763 0.705783 mangoes fruit
24 Fresh1 3.213494 0.493835 0.396832 2.582272 asparagus vegetables
25 Fresh, consumed with peel1 1.295931 0.970000 0.264555 0.353448 cucumbers vegetables
26 Fresh, peeled1 1.295931 0.730000 0.264555 0.469650 cucumbers vegetables
27 Fresh1 1.213039 0.950000 0.242508 0.309655 lettuce_iceberg vegetables
28 Fresh1 1.038107 0.900000 0.352740 0.406868 onions vegetables
29 Fresh1 2.471749 0.750000 0.319670 1.053526 turnip_greens vegetables
30 Fresh1 2.569235 0.840000 0.308647 0.944032 mustard_greens vegetables
31 Fresh1 0.564320 0.811301 0.264555 0.184017 potatoes vegetables
32 Fresh1 2.630838 1.160000 0.286601 0.650001 collard_greens vegetables
33 Fresh1 2.139972 0.846575 0.275578 0.696606 green_beans vegetables
34 Fresh1 1.172248 0.458554 0.451948 1.155360 acorn_squash vegetables
35 Fresh1 2.277940 0.820000 0.264555 0.734926 red_peppers vegetables
36 Fresh green cabbage1 0.579208 0.778797 0.330693 0.245944 cabbage vegetables
37 Fresh red cabbage1 1.056450 0.779107 0.330693 0.448412 cabbage vegetables
38 Fresh1 0.918897 0.811301 0.440925 0.499400 sweet_potatoes vegetables
39 Fresh1 1.639477 0.769500 0.396832 0.845480 summer_squash vegetables
40 Fresh1 1.311629 0.900000 0.275578 0.401618 radish vegetables
41 Fresh1 1.244737 0.714000 0.451948 0.787893 butternut_squash vegetables
42 Fresh1 2.235874 0.740753 0.319670 0.964886 avocados vegetables
43 Fresh1 2.807302 1.050000 0.286601 0.766262 kale vegetables
44 Fresh1 2.213050 0.375309 0.385809 2.274967 artichoke vegetables
45 Fresh1 3.213552 0.769474 0.352740 1.473146 okra vegetables
46 Fresh1 1.410363 0.820000 0.264555 0.455022 green_peppers vegetables
47 Fresh1 2.763553 1.060000 0.341717 0.890898 brussels_sprouts vegetables
48 Fresh1 2.690623 0.540000 0.363763 1.812497 corn_sweet vegetables
In [161]:
l = {}
for j in range(0, len(data)):
    x = ndb_search(data['food'][j]) 
    for i in range(0, len(x)):
        if 'raw' in x[i]['name']:
            l[data['food'][j]] = x[i]['ndbno']
            break
In [162]:
df = pd.DataFrame(l.items())
df.columns = ['food', 'ndb_num']
In [163]:
df = df.merge(data)
df
Out[163]:
food ndb_num form price_per_lb yield lb_per_cup price_per_cup type
0 bananas 09040 Fresh1 0.566983 0.640000 0.330693 0.292965 fruit
1 lettuce_iceberg 11252 Fresh1 1.213039 0.950000 0.242508 0.309655 vegetables
2 mangoes 09176 Fresh1 1.377563 0.710000 0.363763 0.705783 fruit
3 corn_sweet 11900 Fresh1 2.690623 0.540000 0.363763 1.812497 vegetables
4 peaches 09236 Fresh1 1.591187 0.960000 0.341717 0.566390 fruit
5 turnip_greens 11568 Fresh1 2.471749 0.750000 0.319670 1.053526 vegetables
6 artichoke 11007 Fresh1 2.213050 0.375309 0.385809 2.274967 vegetables
7 onions 11282 Fresh1 1.038107 0.900000 0.352740 0.406868 vegetables
8 kale 11233 Fresh1 2.807302 1.050000 0.286601 0.766262 vegetables
9 blueberries 09050 Fresh1 4.734622 0.950000 0.319670 1.593177 fruit
10 honeydew 09184 Fresh1 0.796656 0.460000 0.374786 0.649077 fruit
11 okra 11278 Fresh1 3.213552 0.769474 0.352740 1.473146 vegetables
12 mustard_greens 11270 Fresh1 2.569235 0.840000 0.308647 0.944032 vegetables
13 pears 09252 Fresh1 1.461575 0.900000 0.363763 0.590740 fruit
14 apricots 09021 Fresh1 3.040072 0.930000 0.363763 1.189102 fruit
15 cherries 09063 Fresh1 3.592990 0.920000 0.341717 1.334548 fruit
16 nectarines 09191 Fresh1 1.761148 0.910000 0.319670 0.618667 fruit
17 radish 11429 Fresh1 1.311629 0.900000 0.275578 0.401618 vegetables
18 brussels_sprouts 11098 Fresh1 2.763553 1.060000 0.341717 0.890898 vegetables
19 avocados 09038 Fresh1 2.235874 0.740753 0.319670 0.964886 vegetables
20 plums 09279 Fresh1 1.827416 0.940000 0.363763 0.707176 fruit
21 sweet_potatoes 11505 Fresh1 0.918897 0.811301 0.440925 0.499400 vegetables
22 kiwi 14161 Fresh1 2.044683 0.760000 0.385809 1.037970 fruit
23 cantaloupe 09181 Fresh1 0.535874 0.510000 0.374786 0.393800 fruit
24 papaya 09226 Fresh1 1.298012 0.620000 0.308647 0.646174 fruit
25 blackberries 09042 Fresh1 5.774708 0.960000 0.319670 1.922919 fruit
26 pomegranate 09286 Fresh1 2.173590 0.560000 0.341717 1.326342 fruit
27 oranges 09201 Fresh1 1.035173 0.730000 0.407855 0.578357 fruit
28 raspberries 09302 Fresh1 6.975811 0.960000 0.319670 2.322874 fruit
29 strawberries 09316 Fresh1 2.358808 0.940000 0.319670 0.802171 fruit
30 watermelon 09326 Fresh1 0.333412 0.520000 0.330693 0.212033 fruit
31 grapes 09129 Fresh1 2.093827 0.960000 0.330693 0.721266 fruit
32 cabbage 11503 Fresh green cabbage1 0.579208 0.778797 0.330693 0.245944 vegetables
33 cabbage 11503 Fresh red cabbage1 1.056450 0.779107 0.330693 0.448412 vegetables
34 grapefruit 09117 Fresh1 0.897802 0.490000 0.462971 0.848278 fruit
35 asparagus 11011 Fresh1 3.213494 0.493835 0.396832 2.582272 vegetables
36 potatoes 11352 Fresh1 0.564320 0.811301 0.264555 0.184017 vegetables
37 cucumbers 11205 Fresh, consumed with peel1 1.295931 0.970000 0.264555 0.353448 vegetables
38 cucumbers 11205 Fresh, peeled1 1.295931 0.730000 0.264555 0.469650 vegetables
39 apples 09003 Fresh1 1.567515 0.900000 0.242508 0.422373 fruit
40 tangerines 09218 Fresh1 1.377962 0.740000 0.407855 0.759471 fruit
41 pineapple 09266 Fresh1 0.627662 0.510000 0.363763 0.447686 fruit

Exercise 1.3. Read the food reports V2 documentation, then write a function called ndb_report() that requests a basic food report. The function should accept the NDB number as an argument and return the list of nutrients for the food.

Note that the report url is: https://api.nal.usda.gov/ndb/V2/reports

For example, for "09279" (raw plums) the first element of the returned list should be:

{u'group': u'Proximates',
 u'measures': [{u'eqv': 165.0,
   u'eunit': u'g',
   u'label': u'cup, sliced',
   u'qty': 1.0,
   u'value': u'143.93'},
  {u'eqv': 66.0,
   u'eunit': u'g',
   u'label': u'fruit (2-1/8" dia)',
   u'qty': 1.0,
   u'value': u'57.57'},
  {u'eqv': 151.0,
   u'eunit': u'g',
   u'label': u'NLEA serving',
   u'qty': 1.0,
   u'value': u'131.72'}],
 u'name': u'Water',
 u'nutrient_id': u'255',
 u'unit': u'g',
 u'value': u'87.23'}

Be sure to document and test your function.

In [164]:
def ndb_report(num):
    url = "https://api.nal.usda.gov/ndb/V2/reports?ndbno="
    url = url + num + "&type=b" + "&format=json&api_key=" + key
    req = requests.get(url)
    x = req.json()
    return x['foods'][0]['food']['nutrients']
In [165]:
ndb_report("09279")
Out[165]:
[{u'group': u'Proximates',
  u'measures': [{u'eqv': 165.0,
    u'eunit': u'g',
    u'label': u'cup, sliced',
    u'qty': 1.0,
    u'value': u'143.93'},
   {u'eqv': 66.0,
    u'eunit': u'g',
    u'label': u'fruit (2-1/8" dia)',
    u'qty': 1.0,
    u'value': u'57.57'},
   {u'eqv': 151.0,
    u'eunit': u'g',
    u'label': u'NLEA serving',
    u'qty': 1.0,
    u'value': u'131.72'}],
  u'name': u'Water',
  u'nutrient_id': u'255',
  u'unit': u'g',
  u'value': u'87.23'},
 {u'group': u'Proximates',
  u'measures': [{u'eqv': 165.0,
    u'eunit': u'g',
    u'label': u'cup, sliced',
    u'qty': 1.0,
    u'value': u'76'},
   {u'eqv': 66.0,
    u'eunit': u'g',
    u'label': u'fruit (2-1/8" dia)',
    u'qty': 1.0,
    u'value': u'30'},
   {u'eqv': 151.0,
    u'eunit': u'g',
    u'label': u'NLEA serving',
    u'qty': 1.0,
    u'value': u'69'}],
  u'name': u'Energy',
  u'nutrient_id': u'208',
  u'unit': u'kcal',
  u'value': u'46'},
 {u'group': u'Proximates',
  u'measures': [{u'eqv': 165.0,
    u'eunit': u'g',
    u'label': u'cup, sliced',
    u'qty': 1.0,
    u'value': u'1.15'},
   {u'eqv': 66.0,
    u'eunit': u'g',
    u'label': u'fruit (2-1/8" dia)',
    u'qty': 1.0,
    u'value': u'0.46'},
   {u'eqv': 151.0,
    u'eunit': u'g',
    u'label': u'NLEA serving',
    u'qty': 1.0,
    u'value': u'1.06'}],
  u'name': u'Protein',
  u'nutrient_id': u'203',
  u'unit': u'g',
  u'value': u'0.70'},
 {u'group': u'Proximates',
  u'measures': [{u'eqv': 165.0,
    u'eunit': u'g',
    u'label': u'cup, sliced',
    u'qty': 1.0,
    u'value': u'0.46'},
   {u'eqv': 66.0,
    u'eunit': u'g',
    u'label': u'fruit (2-1/8" dia)',
    u'qty': 1.0,
    u'value': u'0.18'},
   {u'eqv': 151.0,
    u'eunit': u'g',
    u'label': u'NLEA serving',
    u'qty': 1.0,
    u'value': u'0.42'}],
  u'name': u'Total lipid (fat)',
  u'nutrient_id': u'204',
  u'unit': u'g',
  u'value': u'0.28'},
 {u'group': u'Proximates',
  u'measures': [{u'eqv': 165.0,
    u'eunit': u'g',
    u'label': u'cup, sliced',
    u'qty': 1.0,
    u'value': u'18.84'},
   {u'eqv': 66.0,
    u'eunit': u'g',
    u'label': u'fruit (2-1/8" dia)',
    u'qty': 1.0,
    u'value': u'7.54'},
   {u'eqv': 151.0,
    u'eunit': u'g',
    u'label': u'NLEA serving',
    u'qty': 1.0,
    u'value': u'17.24'}],
  u'name': u'Carbohydrate, by difference',
  u'nutrient_id': u'205',
  u'unit': u'g',
  u'value': u'11.42'},
 {u'group': u'Proximates',
  u'measures': [{u'eqv': 165.0,
    u'eunit': u'g',
    u'label': u'cup, sliced',
    u'qty': 1.0,
    u'value': u'2.3'},
   {u'eqv': 66.0,
    u'eunit': u'g',
    u'label': u'fruit (2-1/8" dia)',
    u'qty': 1.0,
    u'value': u'0.9'},
   {u'eqv': 151.0,
    u'eunit': u'g',
    u'label': u'NLEA serving',
    u'qty': 1.0,
    u'value': u'2.1'}],
  u'name': u'Fiber, total dietary',
  u'nutrient_id': u'291',
  u'unit': u'g',
  u'value': u'1.4'},
 {u'group': u'Proximates',
  u'measures': [{u'eqv': 165.0,
    u'eunit': u'g',
    u'label': u'cup, sliced',
    u'qty': 1.0,
    u'value': u'16.37'},
   {u'eqv': 66.0,
    u'eunit': u'g',
    u'label': u'fruit (2-1/8" dia)',
    u'qty': 1.0,
    u'value': u'6.55'},
   {u'eqv': 151.0,
    u'eunit': u'g',
    u'label': u'NLEA serving',
    u'qty': 1.0,
    u'value': u'14.98'}],
  u'name': u'Sugars, total',
  u'nutrient_id': u'269',
  u'unit': u'g',
  u'value': u'9.92'},
 {u'group': u'Minerals',
  u'measures': [{u'eqv': 165.0,
    u'eunit': u'g',
    u'label': u'cup, sliced',
    u'qty': 1.0,
    u'value': u'10'},
   {u'eqv': 66.0,
    u'eunit': u'g',
    u'label': u'fruit (2-1/8" dia)',
    u'qty': 1.0,
    u'value': u'4'},
   {u'eqv': 151.0,
    u'eunit': u'g',
    u'label': u'NLEA serving',
    u'qty': 1.0,
    u'value': u'9'}],
  u'name': u'Calcium, Ca',
  u'nutrient_id': u'301',
  u'unit': u'mg',
  u'value': u'6'},
 {u'group': u'Minerals',
  u'measures': [{u'eqv': 165.0,
    u'eunit': u'g',
    u'label': u'cup, sliced',
    u'qty': 1.0,
    u'value': u'0.28'},
   {u'eqv': 66.0,
    u'eunit': u'g',
    u'label': u'fruit (2-1/8" dia)',
    u'qty': 1.0,
    u'value': u'0.11'},
   {u'eqv': 151.0,
    u'eunit': u'g',
    u'label': u'NLEA serving',
    u'qty': 1.0,
    u'value': u'0.26'}],
  u'name': u'Iron, Fe',
  u'nutrient_id': u'303',
  u'unit': u'mg',
  u'value': u'0.17'},
 {u'group': u'Minerals',
  u'measures': [{u'eqv': 165.0,
    u'eunit': u'g',
    u'label': u'cup, sliced',
    u'qty': 1.0,
    u'value': u'12'},
   {u'eqv': 66.0,
    u'eunit': u'g',
    u'label': u'fruit (2-1/8" dia)',
    u'qty': 1.0,
    u'value': u'5'},
   {u'eqv': 151.0,
    u'eunit': u'g',
    u'label': u'NLEA serving',
    u'qty': 1.0,
    u'value': u'11'}],
  u'name': u'Magnesium, Mg',
  u'nutrient_id': u'304',
  u'unit': u'mg',
  u'value': u'7'},
 {u'group': u'Minerals',
  u'measures': [{u'eqv': 165.0,
    u'eunit': u'g',
    u'label': u'cup, sliced',
    u'qty': 1.0,
    u'value': u'26'},
   {u'eqv': 66.0,
    u'eunit': u'g',
    u'label': u'fruit (2-1/8" dia)',
    u'qty': 1.0,
    u'value': u'11'},
   {u'eqv': 151.0,
    u'eunit': u'g',
    u'label': u'NLEA serving',
    u'qty': 1.0,
    u'value': u'24'}],
  u'name': u'Phosphorus, P',
  u'nutrient_id': u'305',
  u'unit': u'mg',
  u'value': u'16'},
 {u'group': u'Minerals',
  u'measures': [{u'eqv': 165.0,
    u'eunit': u'g',
    u'label': u'cup, sliced',
    u'qty': 1.0,
    u'value': u'259'},
   {u'eqv': 66.0,
    u'eunit': u'g',
    u'label': u'fruit (2-1/8" dia)',
    u'qty': 1.0,
    u'value': u'104'},
   {u'eqv': 151.0,
    u'eunit': u'g',
    u'label': u'NLEA serving',
    u'qty': 1.0,
    u'value': u'237'}],
  u'name': u'Potassium, K',
  u'nutrient_id': u'306',
  u'unit': u'mg',
  u'value': u'157'},
 {u'group': u'Minerals',
  u'measures': [{u'eqv': 165.0,
    u'eunit': u'g',
    u'label': u'cup, sliced',
    u'qty': 1.0,
    u'value': u'0'},
   {u'eqv': 66.0,
    u'eunit': u'g',
    u'label': u'fruit (2-1/8" dia)',
    u'qty': 1.0,
    u'value': u'0'},
   {u'eqv': 151.0,
    u'eunit': u'g',
    u'label': u'NLEA serving',
    u'qty': 1.0,
    u'value': u'0'}],
  u'name': u'Sodium, Na',
  u'nutrient_id': u'307',
  u'unit': u'mg',
  u'value': u'0'},
 {u'group': u'Minerals',
  u'measures': [{u'eqv': 165.0,
    u'eunit': u'g',
    u'label': u'cup, sliced',
    u'qty': 1.0,
    u'value': u'0.17'},
   {u'eqv': 66.0,
    u'eunit': u'g',
    u'label': u'fruit (2-1/8" dia)',
    u'qty': 1.0,
    u'value': u'0.07'},
   {u'eqv': 151.0,
    u'eunit': u'g',
    u'label': u'NLEA serving',
    u'qty': 1.0,
    u'value': u'0.15'}],
  u'name': u'Zinc, Zn',
  u'nutrient_id': u'309',
  u'unit': u'mg',
  u'value': u'0.10'},
 {u'group': u'Vitamins',
  u'measures': [{u'eqv': 165.0,
    u'eunit': u'g',
    u'label': u'cup, sliced',
    u'qty': 1.0,
    u'value': u'15.7'},
   {u'eqv': 66.0,
    u'eunit': u'g',
    u'label': u'fruit (2-1/8" dia)',
    u'qty': 1.0,
    u'value': u'6.3'},
   {u'eqv': 151.0,
    u'eunit': u'g',
    u'label': u'NLEA serving',
    u'qty': 1.0,
    u'value': u'14.3'}],
  u'name': u'Vitamin C, total ascorbic acid',
  u'nutrient_id': u'401',
  u'unit': u'mg',
  u'value': u'9.5'},
 {u'group': u'Vitamins',
  u'measures': [{u'eqv': 165.0,
    u'eunit': u'g',
    u'label': u'cup, sliced',
    u'qty': 1.0,
    u'value': u'0.046'},
   {u'eqv': 66.0,
    u'eunit': u'g',
    u'label': u'fruit (2-1/8" dia)',
    u'qty': 1.0,
    u'value': u'0.018'},
   {u'eqv': 151.0,
    u'eunit': u'g',
    u'label': u'NLEA serving',
    u'qty': 1.0,
    u'value': u'0.042'}],
  u'name': u'Thiamin',
  u'nutrient_id': u'404',
  u'unit': u'mg',
  u'value': u'0.028'},
 {u'group': u'Vitamins',
  u'measures': [{u'eqv': 165.0,
    u'eunit': u'g',
    u'label': u'cup, sliced',
    u'qty': 1.0,
    u'value': u'0.043'},
   {u'eqv': 66.0,
    u'eunit': u'g',
    u'label': u'fruit (2-1/8" dia)',
    u'qty': 1.0,
    u'value': u'0.017'},
   {u'eqv': 151.0,
    u'eunit': u'g',
    u'label': u'NLEA serving',
    u'qty': 1.0,
    u'value': u'0.039'}],
  u'name': u'Riboflavin',
  u'nutrient_id': u'405',
  u'unit': u'mg',
  u'value': u'0.026'},
 {u'group': u'Vitamins',
  u'measures': [{u'eqv': 165.0,
    u'eunit': u'g',
    u'label': u'cup, sliced',
    u'qty': 1.0,
    u'value': u'0.688'},
   {u'eqv': 66.0,
    u'eunit': u'g',
    u'label': u'fruit (2-1/8" dia)',
    u'qty': 1.0,
    u'value': u'0.275'},
   {u'eqv': 151.0,
    u'eunit': u'g',
    u'label': u'NLEA serving',
    u'qty': 1.0,
    u'value': u'0.630'}],
  u'name': u'Niacin',
  u'nutrient_id': u'406',
  u'unit': u'mg',
  u'value': u'0.417'},
 {u'group': u'Vitamins',
  u'measures': [{u'eqv': 165.0,
    u'eunit': u'g',
    u'label': u'cup, sliced',
    u'qty': 1.0,
    u'value': u'0.048'},
   {u'eqv': 66.0,
    u'eunit': u'g',
    u'label': u'fruit (2-1/8" dia)',
    u'qty': 1.0,
    u'value': u'0.019'},
   {u'eqv': 151.0,
    u'eunit': u'g',
    u'label': u'NLEA serving',
    u'qty': 1.0,
    u'value': u'0.044'}],
  u'name': u'Vitamin B-6',
  u'nutrient_id': u'415',
  u'unit': u'mg',
  u'value': u'0.029'},
 {u'group': u'Vitamins',
  u'measures': [{u'eqv': 165.0,
    u'eunit': u'g',
    u'label': u'cup, sliced',
    u'qty': 1.0,
    u'value': u'8'},
   {u'eqv': 66.0,
    u'eunit': u'g',
    u'label': u'fruit (2-1/8" dia)',
    u'qty': 1.0,
    u'value': u'3'},
   {u'eqv': 151.0,
    u'eunit': u'g',
    u'label': u'NLEA serving',
    u'qty': 1.0,
    u'value': u'8'}],
  u'name': u'Folate, DFE',
  u'nutrient_id': u'435',
  u'unit': u'\xb5g',
  u'value': u'5'},
 {u'group': u'Vitamins',
  u'measures': [{u'eqv': 165.0,
    u'eunit': u'g',
    u'label': u'cup, sliced',
    u'qty': 1.0,
    u'value': u'0.00'},
   {u'eqv': 66.0,
    u'eunit': u'g',
    u'label': u'fruit (2-1/8" dia)',
    u'qty': 1.0,
    u'value': u'0.00'},
   {u'eqv': 151.0,
    u'eunit': u'g',
    u'label': u'NLEA serving',
    u'qty': 1.0,
    u'value': u'0.00'}],
  u'name': u'Vitamin B-12',
  u'nutrient_id': u'418',
  u'unit': u'\xb5g',
  u'value': u'0.00'},
 {u'group': u'Vitamins',
  u'measures': [{u'eqv': 165.0,
    u'eunit': u'g',
    u'label': u'cup, sliced',
    u'qty': 1.0,
    u'value': u'28'},
   {u'eqv': 66.0,
    u'eunit': u'g',
    u'label': u'fruit (2-1/8" dia)',
    u'qty': 1.0,
    u'value': u'11'},
   {u'eqv': 151.0,
    u'eunit': u'g',
    u'label': u'NLEA serving',
    u'qty': 1.0,
    u'value': u'26'}],
  u'name': u'Vitamin A, RAE',
  u'nutrient_id': u'320',
  u'unit': u'\xb5g',
  u'value': u'17'},
 {u'group': u'Vitamins',
  u'measures': [{u'eqv': 165.0,
    u'eunit': u'g',
    u'label': u'cup, sliced',
    u'qty': 1.0,
    u'value': u'569'},
   {u'eqv': 66.0,
    u'eunit': u'g',
    u'label': u'fruit (2-1/8" dia)',
    u'qty': 1.0,
    u'value': u'228'},
   {u'eqv': 151.0,
    u'eunit': u'g',
    u'label': u'NLEA serving',
    u'qty': 1.0,
    u'value': u'521'}],
  u'name': u'Vitamin A, IU',
  u'nutrient_id': u'318',
  u'unit': u'IU',
  u'value': u'345'},
 {u'group': u'Vitamins',
  u'measures': [{u'eqv': 165.0,
    u'eunit': u'g',
    u'label': u'cup, sliced',
    u'qty': 1.0,
    u'value': u'0.43'},
   {u'eqv': 66.0,
    u'eunit': u'g',
    u'label': u'fruit (2-1/8" dia)',
    u'qty': 1.0,
    u'value': u'0.17'},
   {u'eqv': 151.0,
    u'eunit': u'g',
    u'label': u'NLEA serving',
    u'qty': 1.0,
    u'value': u'0.39'}],
  u'name': u'Vitamin E (alpha-tocopherol)',
  u'nutrient_id': u'323',
  u'unit': u'mg',
  u'value': u'0.26'},
 {u'group': u'Vitamins',
  u'measures': [{u'eqv': 165.0,
    u'eunit': u'g',
    u'label': u'cup, sliced',
    u'qty': 1.0,
    u'value': u'0.0'},
   {u'eqv': 66.0,
    u'eunit': u'g',
    u'label': u'fruit (2-1/8" dia)',
    u'qty': 1.0,
    u'value': u'0.0'},
   {u'eqv': 151.0,
    u'eunit': u'g',
    u'label': u'NLEA serving',
    u'qty': 1.0,
    u'value': u'0.0'}],
  u'name': u'Vitamin D (D2 + D3)',
  u'nutrient_id': u'328',
  u'unit': u'\xb5g',
  u'value': u'0.0'},
 {u'group': u'Vitamins',
  u'measures': [{u'eqv': 165.0,
    u'eunit': u'g',
    u'label': u'cup, sliced',
    u'qty': 1.0,
    u'value': u'0'},
   {u'eqv': 66.0,
    u'eunit': u'g',
    u'label': u'fruit (2-1/8" dia)',
    u'qty': 1.0,
    u'value': u'0'},
   {u'eqv': 151.0,
    u'eunit': u'g',
    u'label': u'NLEA serving',
    u'qty': 1.0,
    u'value': u'0'}],
  u'name': u'Vitamin D',
  u'nutrient_id': u'324',
  u'unit': u'IU',
  u'value': u'0'},
 {u'group': u'Vitamins',
  u'measures': [{u'eqv': 165.0,
    u'eunit': u'g',
    u'label': u'cup, sliced',
    u'qty': 1.0,
    u'value': u'10.6'},
   {u'eqv': 66.0,
    u'eunit': u'g',
    u'label': u'fruit (2-1/8" dia)',
    u'qty': 1.0,
    u'value': u'4.2'},
   {u'eqv': 151.0,
    u'eunit': u'g',
    u'label': u'NLEA serving',
    u'qty': 1.0,
    u'value': u'9.7'}],
  u'name': u'Vitamin K (phylloquinone)',
  u'nutrient_id': u'430',
  u'unit': u'\xb5g',
  u'value': u'6.4'},
 {u'group': u'Lipids',
  u'measures': [{u'eqv': 165.0,
    u'eunit': u'g',
    u'label': u'cup, sliced',
    u'qty': 1.0,
    u'value': u'0.028'},
   {u'eqv': 66.0,
    u'eunit': u'g',
    u'label': u'fruit (2-1/8" dia)',
    u'qty': 1.0,
    u'value': u'0.011'},
   {u'eqv': 151.0,
    u'eunit': u'g',
    u'label': u'NLEA serving',
    u'qty': 1.0,
    u'value': u'0.026'}],
  u'name': u'Fatty acids, total saturated',
  u'nutrient_id': u'606',
  u'unit': u'g',
  u'value': u'0.017'},
 {u'group': u'Lipids',
  u'measures': [{u'eqv': 165.0,
    u'eunit': u'g',
    u'label': u'cup, sliced',
    u'qty': 1.0,
    u'value': u'0.221'},
   {u'eqv': 66.0,
    u'eunit': u'g',
    u'label': u'fruit (2-1/8" dia)',
    u'qty': 1.0,
    u'value': u'0.088'},
   {u'eqv': 151.0,
    u'eunit': u'g',
    u'label': u'NLEA serving',
    u'qty': 1.0,
    u'value': u'0.202'}],
  u'name': u'Fatty acids, total monounsaturated',
  u'nutrient_id': u'645',
  u'unit': u'g',
  u'value': u'0.134'},
 {u'group': u'Lipids',
  u'measures': [{u'eqv': 165.0,
    u'eunit': u'g',
    u'label': u'cup, sliced',
    u'qty': 1.0,
    u'value': u'0.073'},
   {u'eqv': 66.0,
    u'eunit': u'g',
    u'label': u'fruit (2-1/8" dia)',
    u'qty': 1.0,
    u'value': u'0.029'},
   {u'eqv': 151.0,
    u'eunit': u'g',
    u'label': u'NLEA serving',
    u'qty': 1.0,
    u'value': u'0.066'}],
  u'name': u'Fatty acids, total polyunsaturated',
  u'nutrient_id': u'646',
  u'unit': u'g',
  u'value': u'0.044'},
 {u'group': u'Lipids',
  u'measures': [{u'eqv': 165.0,
    u'eunit': u'g',
    u'label': u'cup, sliced',
    u'qty': 1.0,
    u'value': u'0.000'},
   {u'eqv': 66.0,
    u'eunit': u'g',
    u'label': u'fruit (2-1/8" dia)',
    u'qty': 1.0,
    u'value': u'0.000'},
   {u'eqv': 151.0,
    u'eunit': u'g',
    u'label': u'NLEA serving',
    u'qty': 1.0,
    u'value': u'0.000'}],
  u'name': u'Fatty acids, total trans',
  u'nutrient_id': u'605',
  u'unit': u'g',
  u'value': u'0.000'},
 {u'group': u'Lipids',
  u'measures': [{u'eqv': 165.0,
    u'eunit': u'g',
    u'label': u'cup, sliced',
    u'qty': 1.0,
    u'value': u'0'},
   {u'eqv': 66.0,
    u'eunit': u'g',
    u'label': u'fruit (2-1/8" dia)',
    u'qty': 1.0,
    u'value': u'0'},
   {u'eqv': 151.0,
    u'eunit': u'g',
    u'label': u'NLEA serving',
    u'qty': 1.0,
    u'value': u'0'}],
  u'name': u'Cholesterol',
  u'nutrient_id': u'601',
  u'unit': u'mg',
  u'value': u'0'},
 {u'group': u'Other',
  u'measures': [{u'eqv': 165.0,
    u'eunit': u'g',
    u'label': u'cup, sliced',
    u'qty': 1.0,
    u'value': u'0'},
   {u'eqv': 66.0,
    u'eunit': u'g',
    u'label': u'fruit (2-1/8" dia)',
    u'qty': 1.0,
    u'value': u'0'},
   {u'eqv': 151.0,
    u'eunit': u'g',
    u'label': u'NLEA serving',
    u'qty': 1.0,
    u'value': u'0'}],
  u'name': u'Caffeine',
  u'nutrient_id': u'262',
  u'unit': u'mg',
  u'value': u'0'}]
In [167]:
#TEST TO MAKE SURE THIS ACTUALLY GETS NUTRIENTS YO
listcal = ndb_report("09279")[4]
#int(listcal[u'value'])
listcal 
Out[167]:
{u'group': u'Proximates',
 u'measures': [{u'eqv': 165.0,
   u'eunit': u'g',
   u'label': u'cup, sliced',
   u'qty': 1.0,
   u'value': u'18.84'},
  {u'eqv': 66.0,
   u'eunit': u'g',
   u'label': u'fruit (2-1/8" dia)',
   u'qty': 1.0,
   u'value': u'7.54'},
  {u'eqv': 151.0,
   u'eunit': u'g',
   u'label': u'NLEA serving',
   u'qty': 1.0,
   u'value': u'17.24'}],
 u'name': u'Carbohydrate, by difference',
 u'nutrient_id': u'205',
 u'unit': u'g',
 u'value': u'11.42'}

Exercise 1.4. Which foods provide the best combination of price, yield, and nutrition? You can use kilocalories as a measure of "nutrition" here, but more a detailed analysis is better. Use plots to support your analysis.

In [168]:
def calc_nutrients(num):
    l = {}
    for i in range(0, len(df)):
        cals = ndb_report(df['ndb_num'][i])[num] 
        try:
            l[df['ndb_num'][i]] = float(cals[u'value']) 
        except:
            l[df['ndb_num'][i]] = int(cals[u'value'])
    return l
In [169]:
l = calc_nutrients(1) #for kcal
l = pd.DataFrame(l.items())
l.columns = ['ndb_num', 'kcal']
In [170]:
df = df.merge(l)
In [171]:
l = calc_nutrients(2)
l = pd.DataFrame(l.items())
l.columns = ['ndb_num', 'protein']
In [172]:
df = df.merge(l)
In [173]:
l = calc_nutrients(3)
l = pd.DataFrame(l.items())
l.columns = ['ndb_num', 'fat']
df = df.merge(l)
In [174]:
l = calc_nutrients(4)
l = pd.DataFrame(l.items())
l.columns = ['ndb_num', 'carbs']
df = df.merge(l)
In [175]:
df
Out[175]:
food ndb_num form price_per_lb yield lb_per_cup price_per_cup type kcal protein fat carbs
0 bananas 09040 Fresh1 0.566983 0.640000 0.330693 0.292965 fruit 89.0 1.09 0.33 22.84
1 lettuce_iceberg 11252 Fresh1 1.213039 0.950000 0.242508 0.309655 vegetables 14.0 0.90 0.14 2.97
2 mangoes 09176 Fresh1 1.377563 0.710000 0.363763 0.705783 fruit 60.0 0.82 0.38 14.98
3 corn_sweet 11900 Fresh1 2.690623 0.540000 0.363763 1.812497 vegetables 86.0 3.22 1.18 19.02
4 peaches 09236 Fresh1 1.591187 0.960000 0.341717 0.566390 fruit 39.0 0.91 0.25 9.54
5 turnip_greens 11568 Fresh1 2.471749 0.750000 0.319670 1.053526 vegetables 32.0 1.50 0.30 7.13
6 artichoke 11007 Fresh1 2.213050 0.375309 0.385809 2.274967 vegetables 47.0 3.27 0.15 10.51
7 onions 11282 Fresh1 1.038107 0.900000 0.352740 0.406868 vegetables 40.0 1.10 0.10 9.34
8 kale 11233 Fresh1 2.807302 1.050000 0.286601 0.766262 vegetables 49.0 4.28 0.93 8.75
9 blueberries 09050 Fresh1 4.734622 0.950000 0.319670 1.593177 fruit 57.0 0.74 0.33 14.49
10 honeydew 09184 Fresh1 0.796656 0.460000 0.374786 0.649077 fruit 36.0 0.54 0.14 9.09
11 okra 11278 Fresh1 3.213552 0.769474 0.352740 1.473146 vegetables 33.0 1.93 0.19 7.45
12 mustard_greens 11270 Fresh1 2.569235 0.840000 0.308647 0.944032 vegetables 27.0 2.86 0.42 4.67
13 pears 09252 Fresh1 1.461575 0.900000 0.363763 0.590740 fruit 57.0 0.36 0.14 15.23
14 apricots 09021 Fresh1 3.040072 0.930000 0.363763 1.189102 fruit 48.0 1.40 0.39 11.12
15 cherries 09063 Fresh1 3.592990 0.920000 0.341717 1.334548 fruit 50.0 1.00 0.30 12.18
16 nectarines 09191 Fresh1 1.761148 0.910000 0.319670 0.618667 fruit 44.0 1.06 0.32 10.55
17 radish 11429 Fresh1 1.311629 0.900000 0.275578 0.401618 vegetables 16.0 0.68 0.10 3.40
18 brussels_sprouts 11098 Fresh1 2.763553 1.060000 0.341717 0.890898 vegetables 43.0 3.38 0.30 8.95
19 avocados 09038 Fresh1 2.235874 0.740753 0.319670 0.964886 vegetables 167.0 1.96 15.41 8.64
20 plums 09279 Fresh1 1.827416 0.940000 0.363763 0.707176 fruit 46.0 0.70 0.28 11.42
21 sweet_potatoes 11505 Fresh1 0.918897 0.811301 0.440925 0.499400 vegetables 42.0 2.49 0.51 8.82
22 kiwi 14161 Fresh1 2.044683 0.760000 0.385809 1.037970 fruit 47.0 0.00 0.00 12.26
23 cantaloupe 09181 Fresh1 0.535874 0.510000 0.374786 0.393800 fruit 34.0 0.84 0.19 8.16
24 papaya 09226 Fresh1 1.298012 0.620000 0.308647 0.646174 fruit 43.0 0.47 0.26 10.82
25 blackberries 09042 Fresh1 5.774708 0.960000 0.319670 1.922919 fruit 43.0 1.39 0.49 9.61
26 pomegranate 09286 Fresh1 2.173590 0.560000 0.341717 1.326342 fruit 83.0 1.67 1.17 18.70
27 oranges 09201 Fresh1 1.035173 0.730000 0.407855 0.578357 fruit 49.0 1.04 0.30 11.89
28 raspberries 09302 Fresh1 6.975811 0.960000 0.319670 2.322874 fruit 52.0 1.20 0.65 11.94
29 strawberries 09316 Fresh1 2.358808 0.940000 0.319670 0.802171 fruit 32.0 0.67 0.30 7.68
30 watermelon 09326 Fresh1 0.333412 0.520000 0.330693 0.212033 fruit 30.0 0.61 0.15 7.55
31 grapes 09129 Fresh1 2.093827 0.960000 0.330693 0.721266 fruit 57.0 0.81 0.47 13.93
32 cabbage 11503 Fresh green cabbage1 0.579208 0.778797 0.330693 0.245944 vegetables 19.0 2.60 0.20 3.14
33 cabbage 11503 Fresh red cabbage1 1.056450 0.779107 0.330693 0.448412 vegetables 19.0 2.60 0.20 3.14
34 grapefruit 09117 Fresh1 0.897802 0.490000 0.462971 0.848278 fruit 37.0 0.88 0.10 9.09
35 asparagus 11011 Fresh1 3.213494 0.493835 0.396832 2.582272 vegetables 20.0 2.20 0.12 3.88
36 potatoes 11352 Fresh1 0.564320 0.811301 0.264555 0.184017 vegetables 77.0 2.05 0.09 17.49
37 cucumbers 11205 Fresh, consumed with peel1 1.295931 0.970000 0.264555 0.353448 vegetables 15.0 0.65 0.11 3.63
38 cucumbers 11205 Fresh, peeled1 1.295931 0.730000 0.264555 0.469650 vegetables 15.0 0.65 0.11 3.63
39 apples 09003 Fresh1 1.567515 0.900000 0.242508 0.422373 fruit 52.0 0.26 0.17 13.81
40 tangerines 09218 Fresh1 1.377962 0.740000 0.407855 0.759471 fruit 53.0 0.81 0.31 13.34
41 pineapple 09266 Fresh1 0.627662 0.510000 0.363763 0.447686 fruit 50.0 0.54 0.12 13.12
In [176]:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(df["price_per_lb"], df["yield"], df["kcal"])
ax.set_xlabel('Price Per lb')
ax.set_ylabel('Yield')
ax.set_zlabel('Nutrition in Kcal')
ax.set_title('Price per lb vs. Yield vs. Nutrition (kcal)')                                                                             
plt.show()

From here we can see that one of the most expensive fruits also has one of the highest yields and a low kcal value. The next cell will determine what that fruit is.

In [177]:
x = max(df['price_per_lb'])
df[df['price_per_lb'] == x]
Out[177]:
food ndb_num form price_per_lb yield lb_per_cup price_per_cup type kcal protein fat carbs
28 raspberries 09302 Fresh1 6.975811 0.96 0.31967 2.322874 fruit 52.0 1.2 0.65 11.94

From here we see that raspberries take on the value described above.

In [178]:
plt.scatter(df['kcal'], df['yield'])
plt.xlabel("Nutrition (kcal)")
plt.ylabel("Yield")
plt.title("Nutrition vs. Yield")
plt.show()

From this graph, we see that there is not much of a correlation between nutrition and yield. It seems like nutrition can not directly determine percent yield. There does seem to be an outlier in this data which is the fruit with kcal approximately equal to 160. The next cell will determine which fruit that is.

In [179]:
x = max(df['kcal'])
df[df['kcal'] == x]
Out[179]:
food ndb_num form price_per_lb yield lb_per_cup price_per_cup type kcal protein fat carbs
19 avocados 09038 Fresh1 2.235874 0.740753 0.31967 0.964886 vegetables 167.0 1.96 15.41 8.64

From here we can see that avocados have a highest calories than all of the fruits.

In [180]:
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(df["price_per_lb"], df["yield"], df["protein"])
ax.set_xlabel('Price Per lb')
ax.set_ylabel('Yield')
ax.set_zlabel('Nutrition in Protein')
ax.set_title('Price per lb vs. Yield vs. Nutrition (Protein)')                                                                             
plt.show()

From here we see that the vegetables/fruit with the most protein are the ones that have mid price per pound and have a high yield. The fruit/vegetable with the hightest amount of protein is: kale which has a moderate price per pound and also a very high yield.

In [181]:
x = max(df['protein'])
df[df['protein'] == x]
Out[181]:
food ndb_num form price_per_lb yield lb_per_cup price_per_cup type kcal protein fat carbs
8 kale 11233 Fresh1 2.807302 1.05 0.286601 0.766262 vegetables 49.0 4.28 0.93 8.75
In [182]:
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(df["price_per_lb"], df["yield"], df["fat"])
ax.set_xlabel('Price Per lb')
ax.set_ylabel('Yield')
ax.set_zlabel('Nutrition in fat')
ax.set_title('Price per lb vs. Yield vs. Nutrition (fat)')                                                                             
plt.show()

From here we see that most of the fruit/veggies are low in fat (which makes sense). There is only one value that seems to be an outlier for amount of fat since it is significantly much higher than the rest of the points. This value is fairly inexpensive per pound and has a somewhat high yield. From the output below we see that this value is avocado. This makes sense since avocados are known to be high in fat.

In [183]:
x = max(df['fat'])
df[df['fat']== x]
Out[183]:
food ndb_num form price_per_lb yield lb_per_cup price_per_cup type kcal protein fat carbs
19 avocados 09038 Fresh1 2.235874 0.740753 0.31967 0.964886 vegetables 167.0 1.96 15.41 8.64
In [184]:
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(df["price_per_lb"], df["yield"], df["carbs"])
ax.set_xlabel('Price Per lb')
ax.set_ylabel('Yield')
ax.set_zlabel('Nutrition in carbs')
ax.set_title('Price per lb vs. Yield vs. Nutrition (carbs)')                                                                             
plt.show()

Lastly we have price vs yield vs carbs. There is one value that seems to have the highest amount of carbs. It is fairly cheap and seems to have a moderately low yield in comparison to the other points. The block below will calculate the value with the highest carb content. Thus bananas have the highest amount of carbs from the points collected.

In [185]:
x = max(df['carbs'])
df[df['carbs']== x]
Out[185]:
food ndb_num form price_per_lb yield lb_per_cup price_per_cup type kcal protein fat carbs
0 bananas 09040 Fresh1 0.566983 0.64 0.330693 0.292965 fruit 89.0 1.09 0.33 22.84
In [ ]: