Skip to content

Instantly share code, notes, and snippets.

@shantanuo
Created July 31, 2014 11:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shantanuo/210e11792fca39397739 to your computer and use it in GitHub Desktop.
Save shantanuo/210e11792fca39397739 to your computer and use it in GitHub Desktop.
convert JSON data to csv
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