Public
Edited
Oct 19, 2023
Insert cell
# Munging UN Demographics Data
Insert cell
file = FileAttachment("medium.csv").csv()
Insert cell
fileLow = FileAttachment("low.csv").csv()
Insert cell
fileHigh = FileAttachment("high.csv").csv()
Insert cell
PRIMARY_KEY = "Region, subregion, country or area *"
Insert cell
SECONDARY_KEY="Year"
Insert cell
VARIANT_KEY = "Variant"
Insert cell
LOCATION_CODE_KEY = "Location code"
Insert cell
FULL_YEARS = [2022, 2060, 2100]
Insert cell
VARIANTS = ["Medium", "Low", "High"]
Insert cell
regionKeys = file.reduce((acc, row) => {
const code = parseInt(row[LOCATION_CODE_KEY], 10);
const key = row[PRIMARY_KEY];
if (acc.includes(code) || !row.Year) {
return acc;
}
return acc.concat(code);
}, []);
Insert cell
secondaryKeys = file.reduce((acc, row) => {
const key = parseInt(row[SECONDARY_KEY], 10);
if (acc.includes(key) || isNaN(key)) {
return acc;
}
return acc.concat(key);
}, []);
Insert cell
rows = [...file, ...fileLow, ...fileHigh].map(row => {
const region = row[PRIMARY_KEY];
const variant = row[VARIANT_KEY];
const year = parseInt(row[SECONDARY_KEY]);
const regionKey = parseInt(row[LOCATION_CODE_KEY], 10);
if (isNaN(year)) {
return null
}
const pop = Object.keys(row).map(key => {
if (isNaN(parseInt(key, 10))) {
return null;
}
return parseInt(row[key].split(/\D+/).join(''), 10);
}).filter(val => val !== null);
return { region, variant, year, pop, regionKey }
}).filter(Boolean);
Insert cell
regions = regionKeys.map((key) => {
const row = rows.find(row => row.regionKey === key);
if (!row || !row.pop.reduce((acc, value) => acc + value, 0)) {
return null;
}
const name = row.region;
return {key, name};
}).filter(Boolean);
Insert cell
records = VARIANTS.reduce((acc, variant) => {
const records = regions.map(({ key: regionKey }) => {
const selected = rows.filter(row => row.regionKey === regionKey && row.variant === variant);
const regionName = selected[0].region;
const structure = secondaryKeys.map(year => {
const row = selected.find(row => row.year === year);
const total = row.pop.reduce((acc, value) => acc + value, 0);
return {
year,
amounts: row.pop,
fractions: row.pop.map(value => (value / total) || 0),
total,
dependencyRatio: popToDependencyRatio(row.pop)
}
});
return { variant, regionName: regionName, regionKey: regionKey, structure };
});
return [...acc, ...records];
}, []);
Insert cell
function popToDependencyRatio(pop) {
const dependent = [...pop.slice(0, 16), ...pop.slice(65)].reduce((total, item) => total + item, 0);
const working = pop.slice(16, 65).reduce((total, item) => total + item, 0);
const ratio = dependent / working;
return ratio && !isNaN(ratio) && ratio !== Infinity ? ratio : null;
}
Insert cell
records.filter(record => !record.structure[2].structure[99] === null);
Insert cell
rows.filter(row => row.regionKey === 900 && ['Low', 'High'].includes(row.variant)).map(row => ({
year: row.year,
variant: row.variant,
pop: row.pop.reduce((total, item) => total + item, 0),
}))
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