In [1]:
#Preparing the data
In [2]:
import pandas as pd
In [3]:
pop = pd.read_csv("data/regionpop.csv")
In [4]:
pop.head(2)
Out[4]:
In [5]:
#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 [7]:
import jupyterviz
In [8]:
jupyterviz.viz(type="treemap",data=result)
Out[8]: