Assignment 4: Niveditha Achanta

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:

In [276]:
import requests
import requests_cache
import urlparse
import pandas as pd
import json
import os
import glob
import sys
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
In [277]:
requests_cache.install_cache('demo_cache')

API key: DGg2xJm5VVML7nE45ooZjKhnht2tFk197ayqvxaK

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 [278]:
key = "DGg2xJm5VVML7nE45ooZjKhnht2tFk197ayqvxaK" #your API key goes here
  
def ndb_search(key, term, sort='r', data_source="Standard Reference", group=""):
    """
    accept the search term as an argument and
    return the search result items as a list (for 0 items, return an empty list)
    """
    #set parameters max,offset, and sort
    url = 'https://api.nal.usda.gov/ndb/search'
    response = requests.get(url, params = {
        "q": term,
        "format": "json",
        "sort": sort,
        "ds": data_source,
        "fg": group, 
        "api_key": key
        
    })

    response.raise_for_status() # check for errors
    try:
        list_out = response.json()['list']['item']
    except KeyError:
        list_out=[]
    return list_out
        
ndb_search(key, "","","","")
Out[278]:
[{u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Butter, salted',
  u'ndbno': u'01001',
  u'offset': 0},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Butter, whipped, with salt',
  u'ndbno': u'01002',
  u'offset': 1},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Butter oil, anhydrous',
  u'ndbno': u'01003',
  u'offset': 2},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, blue',
  u'ndbno': u'01004',
  u'offset': 3},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, brick',
  u'ndbno': u'01005',
  u'offset': 4},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, brie',
  u'ndbno': u'01006',
  u'offset': 5},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, camembert',
  u'ndbno': u'01007',
  u'offset': 6},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, caraway',
  u'ndbno': u'01008',
  u'offset': 7},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, cheddar',
  u'ndbno': u'01009',
  u'offset': 8},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, cheshire',
  u'ndbno': u'01010',
  u'offset': 9},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, colby',
  u'ndbno': u'01011',
  u'offset': 10},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, cottage, creamed, large or small curd',
  u'ndbno': u'01012',
  u'offset': 11},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, cottage, creamed, with fruit',
  u'ndbno': u'01013',
  u'offset': 12},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, cottage, nonfat, uncreamed, dry, large or small curd',
  u'ndbno': u'01014',
  u'offset': 13},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, cottage, lowfat, 2% milkfat',
  u'ndbno': u'01015',
  u'offset': 14},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, cottage, lowfat, 1% milkfat',
  u'ndbno': u'01016',
  u'offset': 15},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, cream',
  u'ndbno': u'01017',
  u'offset': 16},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, edam',
  u'ndbno': u'01018',
  u'offset': 17},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, feta',
  u'ndbno': u'01019',
  u'offset': 18},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, fontina',
  u'ndbno': u'01020',
  u'offset': 19},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, gjetost',
  u'ndbno': u'01021',
  u'offset': 20},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, gouda',
  u'ndbno': u'01022',
  u'offset': 21},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, gruyere',
  u'ndbno': u'01023',
  u'offset': 22},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, limburger',
  u'ndbno': u'01024',
  u'offset': 23},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, monterey',
  u'ndbno': u'01025',
  u'offset': 24},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, mozzarella, whole milk',
  u'ndbno': u'01026',
  u'offset': 25},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, mozzarella, whole milk, low moisture',
  u'ndbno': u'01027',
  u'offset': 26},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, mozzarella, part skim milk',
  u'ndbno': u'01028',
  u'offset': 27},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, mozzarella, low moisture, part-skim',
  u'ndbno': u'01029',
  u'offset': 28},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, muenster',
  u'ndbno': u'01030',
  u'offset': 29},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, neufchatel',
  u'ndbno': u'01031',
  u'offset': 30},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, parmesan, grated',
  u'ndbno': u'01032',
  u'offset': 31},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, parmesan, hard',
  u'ndbno': u'01033',
  u'offset': 32},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, port de salut',
  u'ndbno': u'01034',
  u'offset': 33},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, provolone',
  u'ndbno': u'01035',
  u'offset': 34},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, ricotta, whole milk',
  u'ndbno': u'01036',
  u'offset': 35},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, ricotta, part skim milk',
  u'ndbno': u'01037',
  u'offset': 36},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, romano',
  u'ndbno': u'01038',
  u'offset': 37},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, roquefort',
  u'ndbno': u'01039',
  u'offset': 38},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, swiss',
  u'ndbno': u'01040',
  u'offset': 39},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, tilsit',
  u'ndbno': u'01041',
  u'offset': 40},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, pasteurized process, American, fortified with vitamin D',
  u'ndbno': u'01042',
  u'offset': 41},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, pasteurized process, pimento',
  u'ndbno': u'01043',
  u'offset': 42},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, pasteurized process, swiss',
  u'ndbno': u'01044',
  u'offset': 43},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese food, cold pack, American',
  u'ndbno': u'01045',
  u'offset': 44},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese food, pasteurized process, American, vitamin D fortified',
  u'ndbno': u'01046',
  u'offset': 45},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese food, pasteurized process, swiss',
  u'ndbno': u'01047',
  u'offset': 46},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese spread, pasteurized process, American',
  u'ndbno': u'01048',
  u'offset': 47},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cream, fluid, half and half',
  u'ndbno': u'01049',
  u'offset': 48},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cream, fluid, light (coffee cream or table cream)',
  u'ndbno': u'01050',
  u'offset': 49},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cream, fluid, light whipping',
  u'ndbno': u'01052',
  u'offset': 50},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cream, fluid, heavy whipping',
  u'ndbno': u'01053',
  u'offset': 51},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cream, whipped, cream topping, pressurized',
  u'ndbno': u'01054',
  u'offset': 52},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cream, sour, reduced fat, cultured',
  u'ndbno': u'01055',
  u'offset': 53},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cream, sour, cultured',
  u'ndbno': u'01056',
  u'offset': 54},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Eggnog',
  u'ndbno': u'01057',
  u'offset': 55},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Sour dressing, non-butterfat, cultured, filled cream-type',
  u'ndbno': u'01058',
  u'offset': 56},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Milk, filled, fluid, with blend of hydrogenated vegetable oils',
  u'ndbno': u'01059',
  u'offset': 57},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Milk, filled, fluid, with lauric acid oil',
  u'ndbno': u'01060',
  u'offset': 58},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, American, nonfat or fat free',
  u'ndbno': u'01061',
  u'offset': 59},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cream substitute, liquid, with hydrogenated vegetable oil and soy protein',
  u'ndbno': u'01067',
  u'offset': 60},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cream substitute, liquid, with lauric acid oil and sodium caseinate',
  u'ndbno': u'01068',
  u'offset': 61},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cream substitute, powdered',
  u'ndbno': u'01069',
  u'offset': 62},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Dessert topping, powdered',
  u'ndbno': u'01070',
  u'offset': 63},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Dessert topping, powdered, 1.5 ounce prepared with 1/2 cup milk',
  u'ndbno': u'01071',
  u'offset': 64},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Dessert topping, pressurized',
  u'ndbno': u'01072',
  u'offset': 65},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Dessert topping, semi solid, frozen',
  u'ndbno': u'01073',
  u'offset': 66},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Sour cream, imitation, cultured',
  u'ndbno': u'01074',
  u'offset': 67},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Milk substitutes, fluid, with lauric acid oil',
  u'ndbno': u'01076',
  u'offset': 68},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Milk, whole, 3.25% milkfat, with added vitamin D',
  u'ndbno': u'01077',
  u'offset': 69},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Milk, producer, fluid, 3.7% milkfat',
  u'ndbno': u'01078',
  u'offset': 70},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Milk, reduced fat, fluid, 2% milkfat, with added vitamin A and vitamin D',
  u'ndbno': u'01079',
  u'offset': 71},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Milk, reduced fat, fluid, 2% milkfat, with added nonfat milk solids and vitamin A and vitamin D',
  u'ndbno': u'01080',
  u'offset': 72},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Milk, reduced fat, fluid, 2% milkfat, protein fortified, with added vitamin A and vitamin D',
  u'ndbno': u'01081',
  u'offset': 73},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Milk, lowfat, fluid, 1% milkfat, with added vitamin A and vitamin D',
  u'ndbno': u'01082',
  u'offset': 74},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Milk, lowfat, fluid, 1% milkfat, with added nonfat milk solids, vitamin A and vitamin D',
  u'ndbno': u'01083',
  u'offset': 75},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Milk, lowfat, fluid, 1% milkfat, protein fortified, with added vitamin A and vitamin D',
  u'ndbno': u'01084',
  u'offset': 76},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Milk, nonfat, fluid, with added vitamin A and vitamin D (fat free or skim)',
  u'ndbno': u'01085',
  u'offset': 77},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Milk, nonfat, fluid, with added nonfat milk solids, vitamin A and vitamin D (fat free or skim)',
  u'ndbno': u'01086',
  u'offset': 78},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Milk, nonfat, fluid, protein fortified, with added vitamin A and vitamin D (fat free and skim)',
  u'ndbno': u'01087',
  u'offset': 79},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Milk, buttermilk, fluid, cultured, lowfat',
  u'ndbno': u'01088',
  u'offset': 80},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Milk, low sodium, fluid',
  u'ndbno': u'01089',
  u'offset': 81},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Milk, dry, whole, with added vitamin D',
  u'ndbno': u'01090',
  u'offset': 82},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Milk, dry, nonfat, regular, without added vitamin A and vitamin D',
  u'ndbno': u'01091',
  u'offset': 83},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Milk, dry, nonfat, instant, with added vitamin A and vitamin D',
  u'ndbno': u'01092',
  u'offset': 84},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Milk, dry, nonfat, calcium reduced',
  u'ndbno': u'01093',
  u'offset': 85},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Milk, buttermilk, dried',
  u'ndbno': u'01094',
  u'offset': 86},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Milk, canned, condensed, sweetened',
  u'ndbno': u'01095',
  u'offset': 87},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Milk, canned, evaporated, with added vitamin D and without added vitamin A',
  u'ndbno': u'01096',
  u'offset': 88},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Milk, canned, evaporated, nonfat, with added vitamin A and vitamin D',
  u'ndbno': u'01097',
  u'offset': 89},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Milk, chocolate, fluid, commercial, whole, with added vitamin A and vitamin D',
  u'ndbno': u'01102',
  u'offset': 90},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Milk, chocolate, fluid, commercial, reduced fat, with added vitamin A and vitamin D',
  u'ndbno': u'01103',
  u'offset': 91},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Milk, chocolate, lowfat, with added vitamin A and vitamin D',
  u'ndbno': u'01104',
  u'offset': 92},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Milk, chocolate beverage, hot cocoa, homemade',
  u'ndbno': u'01105',
  u'offset': 93},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Milk, goat, fluid, with added vitamin D',
  u'ndbno': u'01106',
  u'offset': 94},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Milk, human, mature, fluid',
  u'ndbno': u'01107',
  u'offset': 95},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Milk, indian buffalo, fluid',
  u'ndbno': u'01108',
  u'offset': 96},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Milk, sheep, fluid',
  u'ndbno': u'01109',
  u'offset': 97},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Milk shakes, thick chocolate',
  u'ndbno': u'01110',
  u'offset': 98},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Milk shakes, thick vanilla',
  u'ndbno': u'01111',
  u'offset': 99},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Whey, acid, fluid',
  u'ndbno': u'01112',
  u'offset': 100},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Whey, acid, dried',
  u'ndbno': u'01113',
  u'offset': 101},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Whey, sweet, fluid',
  u'ndbno': u'01114',
  u'offset': 102},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Whey, sweet, dried',
  u'ndbno': u'01115',
  u'offset': 103},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Yogurt, plain, whole milk, 8 grams protein per 8 ounce',
  u'ndbno': u'01116',
  u'offset': 104},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Yogurt, plain, low fat, 12 grams protein per 8 ounce',
  u'ndbno': u'01117',
  u'offset': 105},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Yogurt, plain, skim milk, 13 grams protein per 8 ounce',
  u'ndbno': u'01118',
  u'offset': 106},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Yogurt, vanilla, low fat, 11 grams protein per 8 ounce',
  u'ndbno': u'01119',
  u'offset': 107},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Yogurt, fruit, low fat, 9 grams protein per 8 ounce',
  u'ndbno': u'01120',
  u'offset': 108},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Yogurt, fruit, low fat, 10 grams protein per 8 ounce',
  u'ndbno': u'01121',
  u'offset': 109},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Yogurt, fruit, low fat, 11 grams protein per 8 ounce',
  u'ndbno': u'01122',
  u'offset': 110},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Egg, whole, raw, fresh',
  u'ndbno': u'01123',
  u'offset': 111},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Egg, white, raw, fresh',
  u'ndbno': u'01124',
  u'offset': 112},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Egg, yolk, raw, fresh',
  u'ndbno': u'01125',
  u'offset': 113},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Egg, yolk, raw, frozen, pasteurized',
  u'ndbno': u'01126',
  u'offset': 114},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Egg, yolk, raw, frozen, sugared, pasteurized',
  u'ndbno': u'01127',
  u'offset': 115},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Egg, whole, cooked, fried',
  u'ndbno': u'01128',
  u'offset': 116},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Egg, whole, cooked, hard-boiled',
  u'ndbno': u'01129',
  u'offset': 117},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Egg, whole, cooked, omelet',
  u'ndbno': u'01130',
  u'offset': 118},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Egg, whole, cooked, poached',
  u'ndbno': u'01131',
  u'offset': 119},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Egg, whole, cooked, scrambled',
  u'ndbno': u'01132',
  u'offset': 120},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Egg, whole, dried',
  u'ndbno': u'01133',
  u'offset': 121},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Egg, whole, dried, stabilized, glucose reduced',
  u'ndbno': u'01134',
  u'offset': 122},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Egg, white, dried, flakes, stabilized, glucose reduced',
  u'ndbno': u'01135',
  u'offset': 123},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Egg, white, dried, powder, stabilized, glucose reduced',
  u'ndbno': u'01136',
  u'offset': 124},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Egg, yolk, dried',
  u'ndbno': u'01137',
  u'offset': 125},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Egg, duck, whole, fresh, raw',
  u'ndbno': u'01138',
  u'offset': 126},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Egg, goose, whole, fresh, raw',
  u'ndbno': u'01139',
  u'offset': 127},
 {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': 128},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Egg, turkey, whole, fresh, raw',
  u'ndbno': u'01141',
  u'offset': 129},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Egg substitute, powder',
  u'ndbno': u'01144',
  u'offset': 130},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Butter, without salt',
  u'ndbno': u'01145',
  u'offset': 131},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, parmesan, shredded',
  u'ndbno': u'01146',
  u'offset': 132},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Milk, nonfat, fluid, without added vitamin A and vitamin D (fat free or skim)',
  u'ndbno': u'01151',
  u'offset': 133},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Milk, reduced fat, fluid, 2% milkfat, with added nonfat milk solids, without added vitamin A',
  u'ndbno': u'01152',
  u'offset': 134},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Milk, canned, evaporated, with added vitamin A',
  u'ndbno': u'01153',
  u'offset': 135},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Milk, dry, nonfat, regular, with added vitamin A and vitamin D',
  u'ndbno': u'01154',
  u'offset': 136},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Milk, dry, nonfat, instant, without added vitamin A and vitamin D',
  u'ndbno': u'01155',
  u'offset': 137},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, goat, hard type',
  u'ndbno': u'01156',
  u'offset': 138},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, goat, semisoft type',
  u'ndbno': u'01157',
  u'offset': 139},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, goat, soft type',
  u'ndbno': u'01159',
  u'offset': 140},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Egg, yolk, raw, frozen, salted, pasteurized',
  u'ndbno': u'01160',
  u'offset': 141},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese substitute, mozzarella',
  u'ndbno': u'01161',
  u'offset': 142},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese sauce, prepared from recipe',
  u'ndbno': u'01164',
  u'offset': 143},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, mexican, queso anejo',
  u'ndbno': u'01165',
  u'offset': 144},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, mexican, queso asadero',
  u'ndbno': u'01166',
  u'offset': 145},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, mexican, queso chihuahua',
  u'ndbno': u'01167',
  u'offset': 146},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, low fat, cheddar or colby',
  u'ndbno': u'01168',
  u'offset': 147},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Cheese, low-sodium, cheddar or colby',
  u'ndbno': u'01169',
  u'offset': 148},
 {u'ds': u'SR',
  u'group': u'Dairy and Egg Products',
  u'name': u'Egg, whole, raw, frozen, pasteurized',
  u'ndbno': u'01171',
  u'offset': 149}]

Comments: Please note that the above code makes provisions for having 6 out of 8 parameters described in the search documentation being dynamic. The only "set" parameters are the format, which we want in JSON, and the api key, which is unique to the user. All other paramters can be changed in the function. The last two, data source and group, are going to become relevant in the next few exercises. From now on, we're going to set data source to Standard Reference, because that's the one we want information for in future problems.

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().

1.2 Strategy Part 1: First, I will import fresh.csv as "fresh." In the first cell, I will rename a column for ease so I can use it in the function I write below. Then, in the next cell, I'm creating a pandas dataframe that contains only fruit and vegetable information from data.gov that contains the word "raw" and falls into "Standard Reference", because this meets the criteria/hints specified. In the third cell, I write a function called match_names() that iterates through the names in the dataframe created from fresh.csv, and matches them with the data.gov dataframe. I use a simple regular expression, because some of the food names contain underscores and other extraneous information that can most effectively be matched through regex. Some issues I encounter through this strategy are discussed below.

In [279]:
#Cell 1
fresh = pd.read_csv('fresh.csv') #Read in CSV 
fresh.rename(columns={'food': 'name', 'type': 'foodtype'}, inplace=True) #Rename columns
#fresh
In [280]:
#Cell 2
fruitdata = pd.io.json.json_normalize(ndb_search(key, "", "r",  "Standard Reference", "Fruits and Fruit Juices"))
vegdata = pd.io.json.json_normalize(ndb_search(key, "", "r", "Standard Reference", "Vegetables and Vegetable Products"))
df = fruitdata.append(vegdata) #append fruit and veg dataframes
df = df.loc[df.name.str.endswith(", raw"), :] #gets only the foods for which the name ends with "raw"
In [281]:
#Cell 3
def match_names(foodname): 
    fresh_names = list(fresh['name'])
    foodname = " ".join(foodname.split("_"))+" raw"
    #print fresh_names
    fn_names = ndb_search(key, foodname)
    try:
        return fn_names[0]['ndbno'] 
    except IndexError: #There is a problem with kiwis, as explained below
        return ""

1.2 Strategy Part 2: Now we want to use this function to create a dataframe that will give us the ndb_nums for every item in fresh. We do that below by making it iterate through every name in fresh.csv, and our final dataframe is returned below.

In [282]:
the_ndbs = [match_names(f) for f in fresh.name] #Iterates through the column "name" in fresh
df_nums = pd.DataFrame(the_ndbs) #Create a dataframe of ndb numbers
frames = (fresh, df_nums)
final_df = pd.concat(frames, axis=1) #Concatenate the df of ndb numbers to the original fresh dataframe
final_df.rename(columns={0L: 'ndb_nums'}, inplace=True)
final_df
Out[282]:
form price_per_lb yield lb_per_cup price_per_cup name foodtype ndb_nums
0 Fresh1 0.333412 0.520000 0.330693 0.212033 watermelon fruit 09326
1 Fresh1 0.535874 0.510000 0.374786 0.393800 cantaloupe fruit 09181
2 Fresh1 1.377962 0.740000 0.407855 0.759471 tangerines fruit 09221
3 Fresh1 2.358808 0.940000 0.319670 0.802171 strawberries fruit 09316
4 Fresh1 1.827416 0.940000 0.363763 0.707176 plums fruit 09279
5 Fresh1 1.035173 0.730000 0.407855 0.578357 oranges fruit 09201
6 Fresh1 6.975811 0.960000 0.319670 2.322874 raspberries fruit 09302
7 Fresh1 2.173590 0.560000 0.341717 1.326342 pomegranate fruit 09286
8 Fresh1 0.627662 0.510000 0.363763 0.447686 pineapple fruit 09266
9 Fresh1 3.040072 0.930000 0.363763 1.189102 apricots fruit 09021
10 Fresh1 0.796656 0.460000 0.374786 0.649077 honeydew fruit 09184
11 Fresh1 1.298012 0.620000 0.308647 0.646174 papaya fruit 09226
12 Fresh1 2.044683 0.760000 0.385809 1.037970 kiwi fruit
13 Fresh1 3.592990 0.920000 0.341717 1.334548 cherries fruit 09276
14 Fresh1 0.566983 0.640000 0.330693 0.292965 bananas fruit 09040
15 Fresh1 1.567515 0.900000 0.242508 0.422373 apples fruit 09003
16 Fresh1 1.591187 0.960000 0.341717 0.566390 peaches fruit 09236
17 Fresh1 1.761148 0.910000 0.319670 0.618667 nectarines fruit 09191
18 Fresh1 1.461575 0.900000 0.363763 0.590740 pears fruit 09252
19 Fresh1 0.897802 0.490000 0.462971 0.848278 grapefruit fruit 09117
20 Fresh1 5.774708 0.960000 0.319670 1.922919 blackberries fruit 09042
21 Fresh1 2.093827 0.960000 0.330693 0.721266 grapes fruit 09129
22 Fresh1 4.734622 0.950000 0.319670 1.593177 blueberries fruit 09050
23 Fresh1 1.377563 0.710000 0.363763 0.705783 mangoes fruit 09176
24 Fresh1 3.213494 0.493835 0.396832 2.582272 asparagus vegetables 11011
25 Fresh, consumed with peel1 1.295931 0.970000 0.264555 0.353448 cucumbers vegetables 11206
26 Fresh, peeled1 1.295931 0.730000 0.264555 0.469650 cucumbers vegetables 11206
27 Fresh1 1.213039 0.950000 0.242508 0.309655 lettuce_iceberg vegetables 11252
28 Fresh1 1.038107 0.900000 0.352740 0.406868 onions vegetables 11282
29 Fresh1 2.471749 0.750000 0.319670 1.053526 turnip_greens vegetables 11568
30 Fresh1 2.569235 0.840000 0.308647 0.944032 mustard_greens vegetables 11270
31 Fresh1 0.564320 0.811301 0.264555 0.184017 potatoes vegetables 11362
32 Fresh1 2.630838 1.160000 0.286601 0.650001 collard_greens vegetables 11161
33 Fresh1 2.139972 0.846575 0.275578 0.696606 green_beans vegetables 11052
34 Fresh1 1.172248 0.458554 0.451948 1.155360 acorn_squash vegetables 11482
35 Fresh1 2.277940 0.820000 0.264555 0.734926 red_peppers vegetables 11821
36 Fresh green cabbage1 0.579208 0.778797 0.330693 0.245944 cabbage vegetables 11503
37 Fresh red cabbage1 1.056450 0.779107 0.330693 0.448412 cabbage vegetables 11503
38 Fresh1 0.918897 0.811301 0.440925 0.499400 sweet_potatoes vegetables 11507
39 Fresh1 1.639477 0.769500 0.396832 0.845480 summer_squash vegetables 11475
40 Fresh1 1.311629 0.900000 0.275578 0.401618 radish vegetables 11429
41 Fresh1 1.244737 0.714000 0.451948 0.787893 butternut_squash vegetables 11485
42 Fresh1 2.235874 0.740753 0.319670 0.964886 avocados vegetables 09038
43 Fresh1 2.807302 1.050000 0.286601 0.766262 kale vegetables 11233
44 Fresh1 2.213050 0.375309 0.385809 2.274967 artichoke vegetables 11226
45 Fresh1 3.213552 0.769474 0.352740 1.473146 okra vegetables 11278
46 Fresh1 1.410363 0.820000 0.264555 0.455022 green_peppers vegetables 11333
47 Fresh1 2.763553 1.060000 0.341717 0.890898 brussels_sprouts vegetables 11098
48 Fresh1 2.690623 0.540000 0.363763 1.812497 corn_sweet vegetables 11900

1.2 Strategy Issues: It needs to be noted that there are a few issues that primarily arise with matching and duplicates. First of all, I don't have an ndb number for kiwis. This is probably because it is named "kiwifruit" in fresh.csv. This can be hardcoded at will, but I'm choosing not to do so, because it won't always be feasible to visually scan the data for issues like it is right now. Similarly, cabbage and cucumbers are both duplicates and both of those rows have the same ndb number even though they refer to different things. There doesn't seem to be a raw red cabbage or a raw green cabbage in the data.gov database, and this function returns the first ndb number for the cabbage list, which in our case is swamp cabbage. It's probably not the one we want, although there only is one type of overall "Cabbage, raw" in data.gov. There is no differentiation across colors. For cucumbers, the function returns the ndb number for "Cucumber, peeled, raw" for both cucumbers. These three issues can be solved by hard-coding, but I think it may be better practices to simply address them here.

The dataframe below, fruitvegdata, has all the information we need, i.e., it contains all the information for fruits and vegetables in fresh.csv. Our next task is to actually find the ndb number of the most relevant result.

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 [283]:
def ndb_report(ndb_num):
    """
    accept the ndb number as an argument and
    return the search result items as a list (for 0 items, return an empty list)
    """
    #set parameters max,offset, and sort
    url = 'https://api.nal.usda.gov/ndb/V2/reports'
    key = "DGg2xJm5VVML7nE45ooZjKhnht2tFk197ayqvxaK"
    response = requests.get(url, params = {
        "ndbno": ndb_num,
        "format": "json",
        "type": "b",
        "api_key": key
        
    })

    response.raise_for_status() # check for errors
    try:
        list_out = response.json()['foods'][0]['food']['nutrients'] #since we want the nutrient information 
    except KeyError:
        list_out=[]
    return list_out
#ndb_report("09279") #for plums
Out[283]:
[{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'}]

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.

1.4 Strategy: When we call the cell below for just one fruit or vegetable (e.g. raw plums, arbitrarily), and specify that we want ["name"], we can get the list of all nutrient names. Similarly, ["value"] returns the actual numeric value of all these nutrients, and this information can be known from looking at the structure of the dictionary that 1.3 returns. After looking at all the names, I chose a few that I thought would be relevant in measuring "nutrition", specifically energy, protein, fiber, and iron. Keep in mind that this is subjective, and is addressed later in "1.4 Strategy Comments" at the end of the assignment.

All the values I chose are graphed below, as well as price and yield information. For some information, I have graphed all the relevant fruits and vegetables, and for some of the more specific nutrient information, I simply picked the top 5 nutrient-rich foods, across both fruits and vegetables. Depending on the specific parameters of the task, we can narrow these down and split into fruits and vegetables, and split it further into different fruit/veggie "groups", but due to time constraints, I look at them all on an aggregate basis.

There are also brief notes after some of the graphs.

In [274]:
#[ndb_report(u'09279')[x]["name"] for x in range(len(ndb_report(u'09279')))] #List of all nutrient names
In [284]:
#Sorry Nick

#Call the value of the specified nutrient in the individual report for each food

energy = [[one_report["value"] for one_report in ndb_report(all_reports) if 'Energy' in one_report["name"]] for all_reports in final_df.ndb_nums]
protein = [[one_report["value"] for one_report in ndb_report(all_reports) if 'Protein' in one_report["name"]] for all_reports in final_df.ndb_nums]
fiber = [[one_report["value"] for one_report in ndb_report(all_reports) if 'Fiber' in one_report["name"]] for all_reports in final_df.ndb_nums]
iron = [[one_report["value"] for one_report in ndb_report(all_reports) if 'Iron' in one_report["name"]] for all_reports in final_df.ndb_nums]
nut_df = pd.DataFrame(
    {'energy': energy,
     'protein': protein,
     'fiber': fiber,
     'iron': iron
    })
nut_df2 = pd.DataFrame([[float(entry[0]) if len(entry)==1 else np.nan for entry in row] for index,row in nut_df.iterrows()])
dfs = (final_df, nut_df2)
final_dfs = pd.concat(dfs, axis=1)
final_dfs.rename(columns={0L: 'energy', 1L: 'protein', 2L: 'fiber', 3L: 'iron'}, inplace=True)
final_dfs #Visualize all the information I want in a dataframe.
Out[284]:
form price_per_lb yield lb_per_cup price_per_cup name foodtype ndb_nums energy protein fiber iron
0 Fresh1 0.333412 0.520000 0.330693 0.212033 watermelon fruit 09326 30.0 0.4 0.24 0.61
1 Fresh1 0.535874 0.510000 0.374786 0.393800 cantaloupe fruit 09181 34.0 0.9 0.21 0.84
2 Fresh1 1.377962 0.740000 0.407855 0.759471 tangerines fruit 09221 43.0 0.2 0.20 0.50
3 Fresh1 2.358808 0.940000 0.319670 0.802171 strawberries fruit 09316 32.0 2.0 0.41 0.67
4 Fresh1 1.827416 0.940000 0.363763 0.707176 plums fruit 09279 46.0 1.4 0.17 0.70
5 Fresh1 1.035173 0.730000 0.407855 0.578357 oranges fruit 09201 49.0 2.5 0.09 1.04
6 Fresh1 6.975811 0.960000 0.319670 2.322874 raspberries fruit 09302 52.0 6.5 0.69 1.20
7 Fresh1 2.173590 0.560000 0.341717 1.326342 pomegranate fruit 09286 83.0 4.0 0.30 1.67
8 Fresh1 0.627662 0.510000 0.363763 0.447686 pineapple fruit 09266 50.0 1.4 0.29 0.54
9 Fresh1 3.040072 0.930000 0.363763 1.189102 apricots fruit 09021 48.0 2.0 0.39 1.40
10 Fresh1 0.796656 0.460000 0.374786 0.649077 honeydew fruit 09184 36.0 0.8 0.17 0.54
11 Fresh1 1.298012 0.620000 0.308647 0.646174 papaya fruit 09226 43.0 1.7 0.25 0.47
12 Fresh1 2.044683 0.760000 0.385809 1.037970 kiwi fruit NaN NaN NaN NaN
13 Fresh1 3.592990 0.920000 0.341717 1.334548 cherries fruit 09276 33.0 NaN 0.20 0.80
14 Fresh1 0.566983 0.640000 0.330693 0.292965 bananas fruit 09040 89.0 2.6 0.26 1.09
15 Fresh1 1.567515 0.900000 0.242508 0.422373 apples fruit 09003 52.0 2.4 0.12 0.26
16 Fresh1 1.591187 0.960000 0.341717 0.566390 peaches fruit 09236 39.0 1.5 0.25 0.91
17 Fresh1 1.761148 0.910000 0.319670 0.618667 nectarines fruit 09191 44.0 1.7 0.28 1.06
18 Fresh1 1.461575 0.900000 0.363763 0.590740 pears fruit 09252 57.0 3.1 0.18 0.36
19 Fresh1 0.897802 0.490000 0.462971 0.848278 grapefruit fruit 09117 37.0 NaN 0.08 0.88
20 Fresh1 5.774708 0.960000 0.319670 1.922919 blackberries fruit 09042 43.0 5.3 0.62 1.39
21 Fresh1 2.093827 0.960000 0.330693 0.721266 grapes fruit 09129 57.0 3.9 0.26 0.81
22 Fresh1 4.734622 0.950000 0.319670 1.593177 blueberries fruit 09050 57.0 2.4 0.28 0.74
23 Fresh1 1.377563 0.710000 0.363763 0.705783 mangoes fruit 09176 60.0 1.6 0.16 0.82
24 Fresh1 3.213494 0.493835 0.396832 2.582272 asparagus vegetables 11011 20.0 2.1 2.14 2.20
25 Fresh, consumed with peel1 1.295931 0.970000 0.264555 0.353448 cucumbers vegetables 11206 12.0 0.7 0.22 0.59
26 Fresh, peeled1 1.295931 0.730000 0.264555 0.469650 cucumbers vegetables 11206 12.0 0.7 0.22 0.59
27 Fresh1 1.213039 0.950000 0.242508 0.309655 lettuce_iceberg vegetables 11252 14.0 1.2 0.41 0.90
28 Fresh1 1.038107 0.900000 0.352740 0.406868 onions vegetables 11282 40.0 1.7 0.21 1.10
29 Fresh1 2.471749 0.750000 0.319670 1.053526 turnip_greens vegetables 11568 32.0 3.2 1.10 1.50
30 Fresh1 2.569235 0.840000 0.308647 0.944032 mustard_greens vegetables 11270 27.0 3.2 1.64 2.86
31 Fresh1 0.564320 0.811301 0.264555 0.184017 potatoes vegetables 11362 58.0 2.5 3.24 2.57
32 Fresh1 2.630838 1.160000 0.286601 0.650001 collard_greens vegetables 11161 32.0 4.0 0.47 3.02
33 Fresh1 2.139972 0.846575 0.275578 0.696606 green_beans vegetables 11052 31.0 2.7 1.03 1.83
34 Fresh1 1.172248 0.458554 0.451948 1.155360 acorn_squash vegetables 11482 40.0 1.5 0.70 0.80
35 Fresh1 2.277940 0.820000 0.264555 0.734926 red_peppers vegetables 11821 31.0 2.1 0.43 0.99
36 Fresh green cabbage1 0.579208 0.778797 0.330693 0.245944 cabbage vegetables 11503 19.0 2.1 1.67 2.60
37 Fresh red cabbage1 1.056450 0.779107 0.330693 0.448412 cabbage vegetables 11503 19.0 2.1 1.67 2.60
38 Fresh1 0.918897 0.811301 0.440925 0.499400 sweet_potatoes vegetables 11507 86.0 3.0 0.61 1.57
39 Fresh1 1.639477 0.769500 0.396832 0.845480 summer_squash vegetables 11475 18.0 1.2 0.40 1.20
40 Fresh1 1.311629 0.900000 0.275578 0.401618 radish vegetables 11429 16.0 1.6 0.34 0.68
41 Fresh1 1.244737 0.714000 0.451948 0.787893 butternut_squash vegetables 11485 45.0 2.0 0.70 1.00
42 Fresh1 2.235874 0.740753 0.319670 0.964886 avocados vegetables 09038 167.0 6.8 0.61 1.96
43 Fresh1 2.807302 1.050000 0.286601 0.766262 kale vegetables 11233 49.0 3.6 1.47 4.28
44 Fresh1 2.213050 0.375309 0.385809 2.274967 artichoke vegetables 11226 73.0 1.6 3.40 2.00
45 Fresh1 3.213552 0.769474 0.352740 1.473146 okra vegetables 11278 33.0 3.2 0.62 1.93
46 Fresh1 1.410363 0.820000 0.264555 0.455022 green_peppers vegetables 11333 20.0 1.7 0.34 0.86
47 Fresh1 2.763553 1.060000 0.341717 0.890898 brussels_sprouts vegetables 11098 43.0 3.8 1.40 3.38
48 Fresh1 2.690623 0.540000 0.363763 1.812497 corn_sweet vegetables 11900 86.0 2.7 0.52 3.22
In [287]:
#Best value  
final_dfs['Price per Yield'] = final_dfs['price_per_cup']/final_dfs['yield']
final_dfs = final_dfs.sort_values(by = "Price per Yield", axis=0, ascending=True, inplace=False, kind='quicksort', na_position='last') 
valplot=sns.factorplot("name", "Price per Yield", data = final_dfs, kind = "bar", size=10) #Barplot
valplot.set_xticklabels(rotation=90) #Rotate axis labels for readability
valplot.fig.suptitle('Food Price (in Dollars) per Yield')
plt.show(valplot)

Notes: First of all, we see that potatoes, cabbage, iceberg lettuce, cucumbers, and watermelon have the best combination of price and yield. We want to look at foods' energy to get a measure of kilocalories. We also want to look at foods' protein, fiber, and iron.

In [288]:
final_dfs = final_dfs.sort_values(by = "energy", axis=0, ascending=True, inplace=False, kind='quicksort', na_position='last') 
calplot=sns.factorplot("name", "energy", data = final_dfs, kind = "bar", size=10) #Barplot
calplot.set_xticklabels(rotation=90) #Rotate axis labels for readability
calplot.fig.suptitle('Energy in Foods')
plt.show(calplot)

Notes: We can see the amount of kilocalories for each food in the plot above. Avocados definitely stand out as having the most kilocalories, and that might be attributed to the amount of fat they have. I'm a bit wary of making conclusions on kilocalories for fruits and vegetables, because in general, calories are not the best measure of nutrition. I will look at the top 5 foods that have the highest protein, fiber, and iron, and see if they are similar and/or if they also overlap with the foods that provide the best price/yield combination.

In [289]:
#Protein 
final_dfs = final_dfs.sort_values(by = "protein", axis=0, ascending=False, inplace=False, kind='quicksort', na_position='last') 
pro=final_dfs[:5:] #Only first five rows
proplot=sns.factorplot("name", "protein", data = pro, kind = "bar", size=5) #Barplot
proplot.set_xticklabels(rotation=90) #Rotate axis labels for readability
proplot.fig.suptitle('Protein in Foods')
plt.show(proplot)
#avocados, raspberries, blackberries, collard greens, pomegranate
In [290]:
#Fiber
final_dfs = final_dfs.sort_values(by = "fiber", axis=0, ascending=False, inplace=False, kind='quicksort', na_position='last') 
fib=final_dfs[:5:] #Only first five rows
fibplot=sns.factorplot("name", "fiber", data = pro, kind = "bar", size=5) #Barplot
fibplot.set_xticklabels(rotation=90) #Rotate axis labels for readability
fibplot.fig.suptitle('Fiber in Foods')
plt.show(fibplot)
#Avocados, raspberries, blackberries, pomegranate, collard greens
In [291]:
#Iron
final_dfs = final_dfs.sort_values(by = "iron", axis=0, ascending=False, inplace=False, kind='quicksort', na_position='last') 
fe=final_dfs[:5:] #Only first five rows
feplot=sns.factorplot("name", "iron", data = fe, kind = "bar", size=5) #Barplot
feplot.set_xticklabels(rotation=90) #Rotate axis labels for readability
feplot.fig.suptitle('Iron in Foods')
plt.show(feplot)
#kale, brussel sprouts, sweet corn, collard greens, mustard greens

1.4 Strategy Comments: The top 5 foods that are high in protein are also the top 5 foods high in fiber, which are avocados, raspberries, blackberries, collard greens, and pomegranates. Out of these, collard greens are also one of the highest in iron, and are on the lower end of the price/yield combination, which means they are maybe the best combination of price, yield, and nutrition. It should be noted that "nutrition" is a very subjective term, and we can make other plots that show correlations and associations of various nutrition information. In my opinion, collard greens stand out, and in general, the best combination of price, yield, and nutrition will tend to lie in the middle of our price/yield vs. foods graph (the very first one). To optimize all these things, you have to determine the tradeoffs you're willing to make, and potentially create some sort of weighted statistical model that can adjust "nutrition" with respect to other criteria.

Again, the issues from 1.2 still apply: we have no information for kiwis and the cabbage and cucumber information might not be exactly what we want it to be. In conclusion, nutrient information can be represented in more detail with more of a theoretical background or further instruction on what is the "best" nutrient, or also with more specifics on the benefits of subsetting this data into fruits, vegetables, fruit types, and vegetable types.

Assignment Information: Bounced ideas off of and received help from #teamdrenched (Hannah Kosinovsky, Chad Pickering, Ricky Safran), and some support from Patrick Vacek and Edie Espejo. As usual, this report would not be possible without Larry Page, Sergei Brin, and the good people from stackoverflow.