In [1]:
#Preparing the data
In [2]:
import pandas as pd
In [3]:
pop = pd.read_csv("data/population.csv")
continent = pd.read_csv("data/continent-mapping.csv")
In [4]:
pop.head(2)
Out[4]:
In [5]:
#we only keep the interesting fields
pop = pop[["Country Name","2014"]]
pop.columns = ["country","population"]
pop.head(2)
Out[5]:
In [6]:
continent.head(2)
Out[6]:
In [8]:
continent = continent[["Country Name","Region"]]
continent.columns=["country","region"]
continent.head(2)
Out[8]:
In [9]:
pop = pop.merge(continent)
pop.head(2)
Out[9]:
In [10]:
open("data/regionpop.csv","w").write(pop.to_csv())
In [11]:
#We convert to a tree
def extractCountries(df):
return df.apply(lambda row: {"name":row["country"],'size':int(row["population"])},1).tolist()
lists = pop.groupby("region").apply(extractCountries).to_dict()
result = {}
result["name"]="World"
result["children"]=[]
for region in lists:
temp = {}
temp["name"] = region
temp["children"] = lists[region]
result["children"].append(temp)
In [12]:
import jupyterviz
In [14]:
jupyterviz.viz(type="circlepack",data=result)
Out[14]:
In [ ]: