{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Transformations\n", "\n", "One important piece of the visualization pipeline is **data transformation**.\n", "\n", "With Altair, you have two possible routes for data transformation; namely:\n", "\n", "1. pre-transformation in Python\n", "2. transformation in Altair/Vega-Lite" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import altair as alt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Calculate Transform\n", "\n", "As an example, let's take a look at transforming some input data that is not encoded in the most intuitive manner.\n", "The ``population`` dataset lists aggregated US census data by year, sex, and age, but lists the sex as \"1\" and \"2\", which makes charts labels not particularly intuitive:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from vega_datasets import data\n", "population = data.population()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | year | \n", "age | \n", "sex | \n", "people | \n", "
---|---|---|---|---|
0 | \n", "1850 | \n", "0 | \n", "1 | \n", "1483789 | \n", "
1 | \n", "1850 | \n", "0 | \n", "2 | \n", "1450376 | \n", "
2 | \n", "1850 | \n", "5 | \n", "1 | \n", "1411067 | \n", "
3 | \n", "1850 | \n", "5 | \n", "2 | \n", "1359668 | \n", "
4 | \n", "1850 | \n", "10 | \n", "1 | \n", "1260099 | \n", "