Published
Edited
Mar 4, 2019
Insert cell
Insert cell
Insert cell
Insert cell
// Loading necessary library here
z = require('https://bundle.run/zebras@0.0.11')
Insert cell
data = d3.csv("https://gist.githubusercontent.com/cesandoval/b834ac93c07e03ec5205843b97f68017/raw/6dff0d45ed26352a7a3c7afb4ace0bad1ce8ba20/MIG_18022019163824069.csv")
Insert cell
Insert cell
// Your code here
z.unique(z.getCol("Country of birth/nationality",data)) // We get 210 unique countries
Insert cell
Insert cell
// Your code here
variable = z.groupBy(x => x.Variable,data) // first grouping by variable

Insert cell
Insert cell
inflows = z.gbSum("Value",z.groupBy(x=> x.CO2,variable['Inflows of foreign population by nationality'])) // then grouping the Inflows by Country...
Insert cell
// Your code here
outflows = z.gbSum("Value",z.groupBy(x=> x.CO2,variable['Outflows of foreign population by nationality'])) // grouping outflows by country...
Insert cell
stock = z.gbSum("Value",z.groupBy(x=> x.CO2,variable['Stock of foreign-born population by country of birth']))
// and finally, grouping stock by country
Insert cell
// looping and pushing the inflows, outflows, and stock into df_summed
df_summed = {
let df = []
for (var i = 0;i<210;i++){
df.push({Country:inflows[i].group,Inflow:inflows[i].sum,Outflow:outflows[i].sum,Stock:stock[i].sum})
}
return df
}

Insert cell
Insert cell
// Your code here
continents = d3.csv("https://gist.githubusercontent.com/cesandoval/b834ac93c07e03ec5205843b97f68017/raw/0c14fa1b1f65a610491417b2894fceecde5f4260/country-and-continent-codes-list-csv_csv.csv")
Insert cell
// Your code here
cont_simple = z.pickCols(['Continent_Name','Three_Letter_Country_Code'],continents) //picking only those two columns
Insert cell
// Your code here
joined = z.merge(df_summed,cont_simple,"Country","Three_Letter_Country_Code","_df1","_df2") //merging by country code
Insert cell
inflow_cont = z.gbSum("Inflow",z.groupBy(r =>r.Continent_Name,joined)) //summing the inflows by continent
Insert cell
outflow_cont = z.gbSum("Outflow",z.groupBy(r =>r.Continent_Name,joined)) //summing the outflows by continent
Insert cell
stock_cont = z.gbSum("Stock",z.groupBy(r =>r.Continent_Name,joined)) //summing the stock by continent
Insert cell
// putting it all together
agg_cont = {
let df = []
for(var i=0;i<7;i++){
df.push({Continent:inflow_cont[i].group,Inflow:inflow_cont[i].sum,Outflow:outflow_cont[i].sum,Stock:stock_cont[i].sum})
}
return df
}
Insert cell
Insert cell
Insert cell
// Your code here
table = z.groupBy(t => t.Variable,data)

Insert cell
// Your code here
//need this intermediate step that only takes year and the variable we want before we can group by country
intermediate = z.filter(r=> r["Year"]==2016 && r["Variable"]=='Stock of foreign-born population by country of birth',data)
Insert cell
// Your code here
//grouping by country and summing the value, which in this case is the variable Stock
stock_16 = z.gbSum("Value",z.groupBy(r=> r['Country of birth/nationality'],intermediate))
Insert cell
z.head(5,stock_16)
Insert cell
lowImmigration = z.filter(r=> r['sum']<=50000,stock_16) // filtering the 3 different buckets
Insert cell
medImmigration = z.filter(r=> r['sum']>50000 && r['sum']<=100000,stock_16)
Insert cell
highImmigration = z.filter(r=> r['sum']>100000,stock_16)
Insert cell
Insert cell
// Your code here
change = z.deriveCol(r=> r.Inflow - r.Outflow,df_summed) //using deriveCol to make a new array
Insert cell
// Your code here
df_inoutFlow = z.addCol("inflow-outflow",change,df_summed) //adding that array to df_summed to make a new array
Insert cell
// Your code here

Insert cell
Insert cell
Insert cell
// Your code here
d3 = require("d3")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more