Created
July 31, 2014 11:20
-
-
Save shantanuo/210e11792fca39397739 to your computer and use it in GitHub Desktop.
convert JSON data to csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pandas import DataFrame, Series | |
import pandas as pd | |
import json | |
from urllib2 import urlopen | |
db = json.load(urlopen('https://github.com/pydata/pydata-book/raw/master/ch07/foods-2011-10-03.json')) | |
info_keys = ['description', 'group', 'id', 'manufacturer'] | |
info = DataFrame(db, columns=info_keys) | |
col_mapping = {'description' : 'food', 'group' : 'fgroup' } | |
info = info.rename(columns=col_mapping, copy=False) | |
nutrients = [] | |
for rec in db: | |
fnuts = DataFrame(rec['nutrients']) | |
fnuts['id'] = rec['id'] | |
nutrients.append(fnuts) | |
nutrients = pd.concat(nutrients, ignore_index=True) | |
nutri_mapping = {'description': 'nutrient', 'group':'nutgroup'} | |
nutrients = nutrients.rename(columns=nutri_mapping, copy=False) | |
ndata = pd.merge(nutrients, info, on='id', how='outer') | |
ndata.to_csv('mytest.csv') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment