import pandas as pd

import json
from pandas.io.json import json_normalize

from numpy import nan

"""
Explanation: JSON examples and exercise


get familiar with packages for dealing with JSON
study examples with JSON strings and files 
work on exercise to be completed and submitted 



reference: http://pandas-docs.github.io/pandas-docs-travis/io.html#json
data source: http://jsonstudio.com/resources/
End of explanation
"""


pd.read_json('data/world_bank_projects.json')['countryname'].value_counts()[:10]


"""
Explanation: JSON exercise
Using data in file 'data/world_bank_projects.json' and the techniques demonstrated above,
1. Find the 10 countries with most projects
2. Find the top 10 major project themes (using column 'mjtheme_namecode')
3. In 2. above you will notice that some entries have only the code and the name is missing. Create a dataframe with the missing names filled in.
Part 1
End of explanation
"""


wb_projects_str=json.load((open('data/world_bank_projects.json')))

themes=json_normalize(wb_projects_str,'mjtheme_namecode')
codecounts=themes['code'].value_counts()
codecounts.name='counts'
codecounts

"""
Explanation: Part 2
End of explanation
"""


codedict=themes.replace('',nan).dropna().groupby('code').last()
codedict

"""
Explanation: Make somthing to tell us what each code means
End of explanation
"""


pd.DataFrame([codedict['name'],codecounts]).transpose().sort('counts',ascending=False)

"""
Explanation: Answer:
Combine the two
End of explanation
"""


%%time
themes['name']=themes['code']


themes['name'].replace(to_replace=codedict.index.values,value=codedict.values,inplace=True)

themes

"""
Explanation: Part 3
Basically we just copy the code column and then replace all the codes with the corresponding name.  This disturbs my sensibilities as it's doing lots more replacements than it has to, but it's faster than anything else I came up with.
End of explanation
"""


