JSON


In [9]:
import numpy as np
import pandas as pd
import os
import sys
import matplotlib
%matplotlib inline
import simplejson as json

from pandas import Series, DataFrame

In [10]:
f = open('../data/ch3/foods.json', 'rb')
json_data = json.load(f)
info_keys = ['description', 'group', 'id']
data = DataFrame(json_data, columns = info_keys)
data.head(20)


Out[10]:
description group id
0 Cheese, caraway Dairy and Egg Products 1008
1 Cheese, cheddar Dairy and Egg Products 1009
2 Cheese, edam Dairy and Egg Products 1018
3 Cheese, feta Dairy and Egg Products 1019
4 Cheese, mozzarella, part skim milk Dairy and Egg Products 1028
5 Cheese, mozzarella, part skim milk, low moisture Dairy and Egg Products 1029
6 Cheese, romano Dairy and Egg Products 1038
7 Cheese, roquefort Dairy and Egg Products 1039
8 Cheese spread, pasteurized process, american, ... Dairy and Egg Products 1048
9 Cream, fluid, half and half Dairy and Egg Products 1049
10 Sour dressing, non-butterfat, cultured, filled... Dairy and Egg Products 1058
11 Milk, filled, fluid, with blend of hydrogenate... Dairy and Egg Products 1059
12 Cream substitute, liquid, with lauric acid oil... Dairy and Egg Products 1068
13 Cream substitute, powdered Dairy and Egg Products 1069
14 Milk, producer, fluid, 3.7% milkfat Dairy and Egg Products 1078
15 Milk, reduced fat, fluid, 2% milkfat, with add... Dairy and Egg Products 1079
16 Milk, reduced fat, fluid, 2% milkfat, with add... Dairy and Egg Products 1080
17 Milk, reduced fat, fluid, 2% milkfat, protein ... Dairy and Egg Products 1081
18 Milk, lowfat, fluid, 1% milkfat, with added vi... Dairy and Egg Products 1082
19 Milk, lowfat, fluid, 1% milkfat, with added no... Dairy and Egg Products 1083

In [11]:
data[0:50].to_json('ch3_out.json', orient = 'records', lines = True)
!cat ch3_out.json


{"description":"Cheese, caraway","group":"Dairy and Egg Products","id":1008}
{"description":"Cheese, cheddar","group":"Dairy and Egg Products","id":1009}
{"description":"Cheese, edam","group":"Dairy and Egg Products","id":1018}
{"description":"Cheese, feta","group":"Dairy and Egg Products","id":1019}
{"description":"Cheese, mozzarella, part skim milk","group":"Dairy and Egg Products","id":1028}
{"description":"Cheese, mozzarella, part skim milk, low moisture","group":"Dairy and Egg Products","id":1029}
{"description":"Cheese, romano","group":"Dairy and Egg Products","id":1038}
{"description":"Cheese, roquefort","group":"Dairy and Egg Products","id":1039}
{"description":"Cheese spread, pasteurized process, american, without di sodium phosphate","group":"Dairy and Egg Products","id":1048}
{"description":"Cream, fluid, half and half","group":"Dairy and Egg Products","id":1049}
{"description":"Sour dressing, non-butterfat, cultured, filled cream-type","group":"Dairy and Egg Products","id":1058}
{"description":"Milk, filled, fluid, with blend of hydrogenated vegetable oils","group":"Dairy and Egg Products","id":1059}
{"description":"Cream substitute, liquid, with lauric acid oil and sodium caseinate","group":"Dairy and Egg Products","id":1068}
{"description":"Cream substitute, powdered","group":"Dairy and Egg Products","id":1069}
{"description":"Milk, producer, fluid, 3.7% milkfat","group":"Dairy and Egg Products","id":1078}
{"description":"Milk, reduced fat, fluid, 2% milkfat, with added vitamin A and vitamin D","group":"Dairy and Egg Products","id":1079}
{"description":"Milk, reduced fat, fluid, 2% milkfat, with added nonfat milk solids and vitamin A and vitamin D","group":"Dairy and Egg Products","id":1080}
{"description":"Milk, reduced fat, fluid, 2% milkfat, protein fortified, with added vitamin A and vitamin D","group":"Dairy and Egg Products","id":1081}
{"description":"Milk, lowfat, fluid, 1% milkfat, with added vitamin A and vitamin D","group":"Dairy and Egg Products","id":1082}
{"description":"Milk, lowfat, fluid, 1% milkfat, with added nonfat milk solids, vitamin A and vitamin D","group":"Dairy and Egg Products","id":1083}
{"description":"Milk, lowfat, fluid, 1% milkfat, protein fortified, with added vitamin A and vitamin D","group":"Dairy and Egg Products","id":1084}
{"description":"Milk, nonfat, fluid, with added vitamin A and vitamin D (fat free or skim)","group":"Dairy and Egg Products","id":1085}
{"description":"Milk, nonfat, fluid, with added nonfat milk solids, vitamin A and vitamin D (fat free or skim)","group":"Dairy and Egg Products","id":1086}
{"description":"Milk, nonfat, fluid, protein fortified, with added vitamin A and vitamin D (fat free and skim)","group":"Dairy and Egg Products","id":1087}
{"description":"Milk, buttermilk, fluid, cultured, lowfat","group":"Dairy and Egg Products","id":1088}
{"description":"Milk, low sodium, fluid","group":"Dairy and Egg Products","id":1089}
{"description":"Milk, dry, whole, with added vitamin D","group":"Dairy and Egg Products","id":1090}
{"description":"Milk, dry, nonfat, regular, without added vitamin A and vitamin D","group":"Dairy and Egg Products","id":1091}
{"description":"Milk, dry, nonfat, instant, with added vitamin A and vitamin D","group":"Dairy and Egg Products","id":1092}
{"description":"Milk, dry, nonfat, calcium reduced","group":"Dairy and Egg Products","id":1093}
{"description":"Milk, buttermilk, dried","group":"Dairy and Egg Products","id":1094}
{"description":"Milk, canned, condensed, sweetened","group":"Dairy and Egg Products","id":1095}
{"description":"Milk, canned, evaporated, with added vitamin D and without added vitamin A","group":"Dairy and Egg Products","id":1096}
{"description":"Milk, canned, evaporated, nonfat, with added vitamin A and vitamin D","group":"Dairy and Egg Products","id":1097}
{"description":"Milk, indian buffalo, fluid","group":"Dairy and Egg Products","id":1108}
{"description":"Milk, sheep, fluid","group":"Dairy and Egg Products","id":1109}
{"description":"Yogurt, plain, skim milk, 13 grams protein per 8 ounce","group":"Dairy and Egg Products","id":1118}
{"description":"Yogurt, vanilla, low fat, 11 grams protein per 8 ounce","group":"Dairy and Egg Products","id":1119}
{"description":"Egg, whole, cooked, fried","group":"Dairy and Egg Products","id":1128}
{"description":"Egg, whole, cooked, hard-boiled","group":"Dairy and Egg Products","id":1129}
{"description":"Egg, duck, whole, fresh, raw","group":"Dairy and Egg Products","id":1138}
{"description":"Egg, goose, whole, fresh, raw","group":"Dairy and Egg Products","id":1139}
{"description":"Cheese, pasteurized process, swiss, without di sodium phosphate","group":"Dairy and Egg Products","id":1148}
{"description":"Cheese food, pasteurized process, american, with di sodium phosphate","group":"Dairy and Egg Products","id":1149}
{"description":"Cheese, goat, soft type","group":"Dairy and Egg Products","id":1159}
{"description":"Cheese, low fat, cheddar or colby","group":"Dairy and Egg Products","id":1168}
{"description":"Cheese, low-sodium, cheddar or colby","group":"Dairy and Egg Products","id":1169}
{"description":"Sour cream, reduced fat","group":"Dairy and Egg Products","id":1178}
{"description":"Sour cream, light","group":"Dairy and Egg Products","id":1179}
{"description":"Sour cream, fat free","group":"Dairy and Egg Products","id":1180}

MySQL


In [12]:
import MySQLdb
import pandas.io.sql as sql

# conn = MySQLdb.conect(host =, user = , passwd =, db =, port =, charset = 'utf8')
# data = sql.read_frame('select * from xxx', conn)
# conn.close()