<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 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
Step1:  imports for Python, Pandas
Step2:  JSON example, with string
demonstrates creation of normalized dataframes (tables) from nested json string
source
Step3:  JSON example, with file
demonstrates reading in a json file as a string and as a table
uses small sample file containing data about projects funded by the World Bank 
data source
Step4:  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.
Data Loading
Step5:  Question 1
Step6:  Question 2
Step7:  Question 3
Step8:  Answer to Q2 without missing values
<ASSISTANT_TASK:>
Python Code:
import pandas as pd
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
import json
from pandas.io.json import json_normalize
Explanation: imports for Python, Pandas
End of explanation
# define json string
data = [{'state': 'Florida', 
         'shortname': 'FL',
         'info': {'governor': 'Rick Scott'},
         'counties': [{'name': 'Dade', 'population': 12345},
                      {'name': 'Broward', 'population': 40000},
                      {'name': 'Palm Beach', 'population': 60000}]},
        {'state': 'Ohio',
         'shortname': 'OH',
         'info': {'governor': 'John Kasich'},
         'counties': [{'name': 'Summit', 'population': 1234},
                      {'name': 'Cuyahoga', 'population': 1337}]}]
# use normalization to create tables from nested element
json_normalize(data, 'counties')
# further populate tables created from nested element
json_normalize(data, 'counties', ['state', 'shortname', ['info', 'governor']])
Explanation: JSON example, with string
demonstrates creation of normalized dataframes (tables) from nested json string
source: http://pandas-docs.github.io/pandas-docs-travis/io.html#normalization
End of explanation
# load json as string
json.load((open('data/world_bank_projects_less.json')))
# load as Pandas dataframe
sample_json_df = pd.read_json('data/world_bank_projects_less.json')
sample_json_df
Explanation: JSON example, with file
demonstrates reading in a json file as a string and as a table
uses small sample file containing data about projects funded by the World Bank 
data source: http://jsonstudio.com/resources/
End of explanation
dataRaw = json.load((open('data/world_bank_projects.json')))
worldBank = json_normalize(dataRaw,'mjtheme_namecode',['countryname',['_id','$oid']])
worldBank = worldBank.rename(columns={'_id.$oid':'oid'})
worldBank.head()
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.
Data Loading:
End of explanation
worldBank.groupby('countryname').oid.nunique().order(ascending=False).head(10)
Explanation: Question 1
End of explanation
worldBank.name.value_counts().head(10)
Explanation: Question 2
End of explanation
from collections import defaultdict
codeNames = defaultdict(str)
for i in range(len(worldBank)):
    if worldBank.name[i] != u'':
        codeNames[worldBank.code[i]] = worldBank.name[i]
for key in codeNames.keys():
    print key,' : ',codeNames[key]
worldBank2 = worldBank
worldBank2['name'] = [codeNames[code] for code in worldBank.code]
worldBank2.head()
Explanation: Question 3
End of explanation
worldBank2.name.value_counts().head(10)
Explanation: Answer to Q2 without missing values
End of explanation
<END_TASK>