Published
Edited
Mar 6, 2019
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
//assign variable to link
mig_url = 'https://gist.githubusercontent.com/cesandoval/b834ac93c07e03ec5205843b97f68017/raw/6dff0d45ed26352a7a3c7afb4ace0bad1ce8ba20/MIG_18022019163824069.csv'
Insert cell
//load to csv array via d3
mig = d3.csv(mig_url)
Insert cell
//import first 5 rows
z.head(200,mig)
Insert cell
//get length of csv
mig.length
Insert cell
//check shape
({rows: mig.length, columns:Object.keys(mig[0]).length})
Insert cell
//check column names
migrationColumns = Object.keys(mig[0])
Insert cell
Insert cell
z.unique(z.getCol('CO2', mig))
Insert cell
Insert cell
migByVariable = z.groupBy(x=>x.Variable, mig)
Insert cell
//sum by inflows by country
inflow_df = z.gbSum("Value", z.groupBy(a => a['CO2'], migByVariable['Inflows of foreign population by nationality']))
Insert cell
outflow_df = z.gbSum("Value", z.groupBy(a => a['CO2'], migByVariable['Outflows of foreign population by nationality']))
Insert cell
z.head(5,inflow_df)
Insert cell
z.head(5,outflow_df)
Insert cell
//sum stock by country
stock_df = z.gbSum("Value", z.groupBy(a => a['CO2'], migByVariable['Stock of foreign-born population by country of birth']))
Insert cell
Insert cell
//get unique countries in dataset
uniqueCountry = z.unique(z.getCol('CO2', mig))
Insert cell
/*{
let outflow_df_format = []
let uniqueCountryOutflow = z.unique(z.getCol('group', outflow_df))
//loop through all 35 values of inflow array; if it's also in the 27 of outflow array, ignore
//if not in the 27, add a row and make sum of that row 0
for(let j=0; j<inflow_df.length; j++){
console.log("evaluating country: "+inflow_df[j].group);
if(uniqueCountryOutflow.indexOf(inflow_df[j].group)<0){
console.log(inflow_df[j].group+ " is not yet in Outflow array")
outflow_df_format.push({['group']:inflow_df[j].group,['sum']:0})
} else {
outflow_df_format.push(inflow_df[j])
}
}
return outflow_df_format
}*/
Insert cell
//everything added
new_df = {
let sum_df = []
//add to the main df
for(var i = 0; i<inflow_df.length; i++){
sum_df.push({['CO2']: inflow_df[i].group, Inflow: inflow_df[i].sum, Outflow: outflow_df[i].sum, Stock: stock_df[i].sum})
}
return sum_df
}
Insert cell
z.head(5,new_df)
Insert cell
Insert cell
//load the data
join_url = 'https://gist.githubusercontent.com/cesandoval/b834ac93c07e03ec5205843b97f68017/raw/0c14fa1b1f65a610491417b2894fceecde5f4260/country-and-continent-codes-list-csv_csv.csv'

Insert cell
df = d3.csv(join_url)
Insert cell
//view the df
z.head(5,new_df)
Insert cell
//pick out two columns from the df
two_df = z.pickCols(['Three_Letter_Country_Code','Continent_Name'],df)
Insert cell
//merge the df and the created columns df
merged_df = z.merge(new_df,two_df,'CO2','Three_Letter_Country_Code',"_df1","_df2")
Insert cell
z.head(10,merged_df)
Insert cell
//count inflows, outflows, and stock by continent
//merged_df_grouped = z.groupBy(a=>a['Continent_Name'],merged_df)
ct_inflow = z.gbSum("Inflow", z.groupBy(a => a['Continent_Name'], merged_df))
Insert cell
ct_outflow = z.gbSum("Outflow", z.groupBy(a => a['Continent_Name'], merged_df))
Insert cell
ct_stock = z.gbSum("Stock", z.groupBy(a => a['Continent_Name'], merged_df))
Insert cell
ct_df = {
let temp = []
//add to the main df
for(var i = 0; i<7; i++){
temp.push({['Continent']: ct_inflow[i].group, Inflow: ct_inflow[i].sum, Outflow: ct_outflow[i].sum, Stock: ct_stock[i].sum})
}
return temp
}
Insert cell
Insert cell
Insert cell
//Create single Zebras table
variablegroup = z.groupBy(a => a.Variable, mig)
Insert cell
filterByYear = z.filter(r=> r["Year"]==2016 && r["Variable"]=='Stock of foreign-born population by country of birth',mig)
Insert cell
//sum and group the 2016 stock by country
stock_2016 = z.gbSum("Value",z.groupBy(r => r['Country of birth/nationality'],filterByYear))
Insert cell
z.head(5,stock_2016)
Insert cell
low_mig = z.filter(r=> r['sum']<=50000,stock_2016)
Insert cell
med_mig = z.filter(r=> r['sum']>50000 && r['sum']<100000,stock_2016)
Insert cell
high_mig = z.filter(r=> r['sum']>=100000,stock_2016)
Insert cell
Insert cell
//make new array using deriveCol
difference = z.deriveCol(r => r.Inflow - r.Outflow,new_df)
Insert cell
inflowOutflow = z.addCol("inflow-outflow",difference,new_df)
Insert cell
z.head(5,inflowOutflow)
Insert cell
Insert cell
Insert cell
// Your code here

Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more