Published
Edited
Mar 6, 2019
Insert cell
Insert cell
Insert cell
Insert cell
// Loading necessary library here
migrationUrl = "https://gist.githubusercontent.com/cesandoval/b834ac93c07e03ec5205843b97f68017/raw/6dff0d45ed26352a7a3c7afb4ace0bad1ce8ba20/MIG_18022019163824069.csv"
Insert cell
data = d3.csv(migrationUrl)
Insert cell
Insert cell
migration = z.parseNums(["Value"], data)
Insert cell
// Your code here: return unique values for "Country of birth/nationality"
z.unique(z.getCol("Country of birth/nationality", migration))
Insert cell
Insert cell
// Your code here
migrationGroups = z.groupBy(r => r.Country, migration) // zebra.groupby returns an object
Insert cell
df_summed = {
/* return a dataframe, df_summed, that each row is unique by country and
for each country, across years:
- total number of "Inflows of foreign population by nationality"
- total number of "Outflows of foreign population by nationality"
- total number of "Stock of foreign-born population by country of birth"
*/
let df = []
let countries = Object.values(migrationGroups) //re-collapse migrationGroups back into an array of dfs
for (let c in countries){
let c_data = countries[c] // c_data is the df containing all data on countries[c]
let c_df = [{"Country": countries[c], "COU": c_data[0].COU}] //create a new df containing 1 row of information on countries[c]
let c_sum = z.gbSum("Value", z.groupBy(d => d.Variable, c_data)) // zebra.groupBySum
let inflow = z.filter(r => r["group"] == "Inflows of foreign population by nationality", c_sum) // unpack
let outflow = z.filter(r => r["group"] == "Outflows of foreign population by nationality", c_sum)
let stock = z.filter(r => r["group"] == "Stock of foreign-born population by country of birth", c_sum)
if (inflow.length > 0){ // in the case that there is data for these values, add column to c_df)
c_df = z.addCol("Inflows of foreign population by nationality", [inflow[0].sum] , c_df)
}
if (outflow.length > 0){
c_df = z.addCol("Outflows of foreign population by nationality", [outflow[0].sum] , c_df)
}
if (stock.length > 0){
c_df = z.addCol("Stock of foreign-born population by country of birth", [stock[0].sum] , c_df)
}
df = z.concat(df, c_df)
}
return df
}
Insert cell
Insert cell
// Your code here
data1 = d3.csv("https://gist.githubusercontent.com/cesandoval/b834ac93c07e03ec5205843b97f68017/raw/0c14fa1b1f65a610491417b2894fceecde5f4260/country-and-continent-codes-list-csv_csv.csv")
Insert cell
// Your code here
df_1 = z.pickCols(["Continent_Name", "Three_Letter_Country_Code"], data1)
Insert cell
Insert cell
continentGroups = z.groupBy(r => r.Continent_Name, df_join)
Insert cell
continents = Object.values(continentGroups)
Insert cell
// Your code here
df_aggregate = {
let continentGroups = z.groupBy(r => r.Continent_Name, df_join)
let continents = Object.values(continentGroups)
let df = []
for (let c in continents){
let c_data = continents[c] // c_data is the df containing all data on continents[c]
let c_df = [{"Continent_Name": continents[c], "Continent_Code": c_data[0].Continent_Code}] //create a new df containing 1 row of information on continents[c]
let inflow = 0
let outflow = 0
let stock = 0
for (let i = 0; i++; i<c_data.length){
inflow += z.unique(z.getCol("Inflows of foreign population by nationality", c_data[i]))
outflow += z.unique(z.getCol("Outflows of foreign population by nationality", c_data[i]))
stock += z.unique(z.getCol("Stock of foreign-born population by country of birth", c_data[i]))
}
if (inflow > 0){ // in the case that there is data for these values, add column to c_df)
c_df = z.addCol("Inflows of foreign population by nationality", [inflow[0].sum] , c_df)
}
if (outflow > 0){
c_df = z.addCol("Outflows of foreign population by nationality", [outflow[0].sum] , c_df)
}
if (stock > 0){
c_df = z.addCol("Stock of foreign-born population by country of birth", [stock[0].sum] , c_df)
}
df = z.concat(df, c_df)
}
return df
}
Insert cell
Insert cell
// Your code here

Insert cell
// Your code here

Insert cell
// Your code here

Insert cell
Insert cell
// Your code here

Insert cell
// Your code here

Insert cell
// Your code here

Insert cell
Insert cell
Insert cell
// Your code here
d3 = require("d3")
Insert cell
z = require("https://bundle.run/zebras@0.0.11")
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