Published
Edited
Mar 7, 2019
Insert cell
Insert cell
Insert cell
Insert cell
migrationURL = "https://gist.githubusercontent.com/cesandoval/b834ac93c07e03ec5205843b97f68017/raw/6dff0d45ed26352a7a3c7afb4ace0bad1ce8ba20/MIG_18022019163824069.csv"
Insert cell
z = require('https://bundle.run/zebras@0.0.11')
Insert cell
d3 = require("d3") // trying to import d3
Insert cell
df = d3.csv(migrationURL)

Insert cell
z.head(10,df)
Insert cell
df
Insert cell
Insert cell
//d3.csv("https://gist.githubusercontent.com/cesandoval/b834ac93c07e03ec5205843b97f68017/raw/6dff0d45ed26352a7a3c7afb4ace0bad1ce8ba20/MIG_18022019163824069.csv")//not working

Insert cell
dfcolumns = Object.keys(df[0])
Insert cell
Insert cell
unique_countries = z.unique(z.getCol('Country of birth/nationality', df))

// Your code here

Insert cell
unique_countries.length
Insert cell
Insert cell
//variablecol = z.getCol('Variable', df)
Insert cell
//countrycol = z.getCol('Country', df)
Insert cell
dfArray = {
let groupedCountries = z.groupBy(r => r.CO2, df)
let dfArray = []
for (const [countryName, value] of Object.entries(groupedCountries)) { //value is everything right of key. objects
let sumByCountry = z.gbSum('Value', z.groupBy(r => r.Variable, value))
let countryRow = {country: countryName} // intiaating an array with a single key as a country name i
for (let i in sumByCountry) {
let groupName = sumByCountry[i].group
let groupValue = sumByCountry[i].sum
countryRow[groupName] = groupValue //the key name of the countryRow is going to be the groupName, and the value of the key is going to be the group value
}
dfArray.push(countryRow)
// console.log(sumByCountry)
}
return dfArray
}
Insert cell
z.head(15, dfArray)
Insert cell
Insert cell
// Other attempts
Insert cell
dfvariablecount = z.valueCounts(z.getCol('Variable', df))

//Gets counts of inflows, outflows, and stock
//how to do this per country?
// Your code here

Insert cell

//dataSorted = z.sortByCol('Variable', 'asc', df)
//ordering rows from largest to smallest values withing a certain column.

Insert cell
//migrationbyCountry = z.groupBy(row=> row.Country, dfvariablecount)
//migrationbyCountry = z.groupBy(row=> row.Country, df)
//groupdf by values that are repeated in a given column. The z.groupBy() function returns an object with the labels of the groups (Countries) as keys and dataframes containing their members as values.
Insert cell
migrationbyVariable = z.groupBy(row=> row.Variable, df) //create an object
Insert cell
// Your code here

Insert cell
Insert cell
/*Q4
You are expected to produce a table where:
every row is a different Continent
Other columns will: sum total Inflows, Outflows, and Stock of foreign population across years, per Continent
*/

Insert cell
newDataURL = "https://gist.githubusercontent.com/cesandoval/b834ac93c07e03ec5205843b97f68017/raw/0c14fa1b1f65a610491417b2894fceecde5f4260/country-and-continent-codes-list-csv_csv.csv"
// Your code here

Insert cell
// Load this data.
df2 = d3.csv("https://gist.githubusercontent.com/cesandoval/b834ac93c07e03ec5205843b97f68017/raw/0c14fa1b1f65a610491417b2894fceecde5f4260/country-and-continent-codes-list-csv_csv.csv")
Insert cell
datakeys = Object.keys(df2[0])

Insert cell
Insert cell

codesContinents = z.pickCols(['Continent_Name', 'Three_Letter_Country_Code'], df2.slice())
// Create a data frame that only keeps the Country Codes and the Continent.
Insert cell
migration_join = z.merge(df, codesContinents, "country", "Three_Letter_Country_Code", "_df1", "_df2")
//attempting to joing the data sets but dont know waht's going wrong
Insert cell
z.print(migration_join)

Insert cell
/*
newdata = {
let CCArray = []
for (var i=0; i<df2.length; i++) {
let currRow = {Continent: continents[i],
Code: countrycodes[i]}
CCArray.push(currRow)
}
return CCArray
}
*/

Insert cell
dfArray.length
Insert cell
df2.length
Insert cell
dfArray2 = {
let groupedContinents = z.groupBy(r => r.Continent, migration_join)
let dfArray = []
for (const [continentName, value] of Object.entries(groupedContinents)) { //value is everything right of key. objects
let sumByContinent = z.gbSum('Value', z.groupBy(r => r.Variable, value))
let continentRow = {continent: continentName} // intiaating an array with a single key as a continent name i
for (let i in sumByContinent) {
let groupName = sumByContinent[i].group
let groupValue = sumByContinent[i].sum
continentRow[groupName] = groupValue //the key name of the countryRow is going to be the groupName, and the value of the key is going to be the group value
}
dfArray.push(continentRow)
// console.log(sumByCountry)
}
return dfArray
}
Insert cell



// Your code here

Insert cell
Insert cell
/*You are expected to produce a 3 tables where:
First, aggregate by Stock of foreign-born population by country of birth

Then you can produce a new table, where every row is a different Country, where you will only keep the data for 2016
For 2016 data:
The high immigration table will have the data of those countries where Stock of foreign-born population by country of birth > 100,000
The medium immigration table will have the data of those countries where Stock of foreign-born population by country of birth > 50,000 & 100,000
The low immigration table will have the data of those countries where Stock of foreign-born population by country of birth < 50,000
*/
Insert cell
//some table, same as above where every row is a country?
Insert cell

filter2016 = z.filter(r=>r['Year']=="2016" && r['Variable']=='Stock of foreign-born population by country of birth', df)
Insert cell
// Aggregate (sum) by country
stockBirthSum2016 = z.gbSum("Value", z.groupBy(d=>d['CO2'], filter2016))
Insert cell
// Filter High Immigration from stockBirthSum2016
highImmigration = z.filter(r=>r['sum']>100000, stockBirthSum2016)
Insert cell
Insert cell
mediumImmigration = z.filter(r=>r['sum']>50000 && r['sum']<100000, stockBirthSum2016)
Insert cell
z.print (mediumImmigration)
Insert cell
lowImmigration = z.filter(r=>r['sum']<50000, stockBirthSum2016)
Insert cell
z.print(lowImmigration)
Insert cell
// Your code here


Insert cell
Insert cell
// Your code here

//first make df with inflow and outflow by country
//then do calculation?
//and save as a new column


Insert cell
Insert cell
Insert cell
outflowSumNation = z.gbSum("Value", z.groupBy(c => c['CO2'],group_variable['Outflows of foreign population by nationality'])) // Sum of outflows by country
Insert cell
migration_join_nation = z.merge(inflowSumNation, outflowSumNation, "group", "group", "_inflow", "_outflow")
Insert cell
migration_join_birth_sub={
let newArray = []
for (var i = 0; i<migration_join_nation.length; i++){
let country = migration_join_nation[i].group; //group refers to the country name in migration_join_birth
newArray.push({country:country, "inflow-Outflow":migration_join_nation[i]['sum_inflow']-migration_join_nation[i]['sum_outflow']});
}
return newArray;
}
Insert cell
Insert cell
Insert cell
z.print(migration_join_nation_inout)
Insert cell
// Your code here

Insert cell
Insert cell
Insert cell
// Your code here

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