Data Transformations#

It is often necessary to transform or filter data in the process of visualizing it. In Altair you can do this one of two ways:

  1. Before the chart definition, using standard Pandas data transformations.

  2. Within the chart definition, using Vega-Lite’s data transformation tools.

In most cases, we suggest that you use the first approach, because it is more straightforward to those who are familiar with data manipulation in Python, and because the Pandas package offers much more flexibility than Vega-Lite in available data manipulations.

The second approach becomes useful when the data source is not a dataframe, but, for example, a URL pointer to a JSON or CSV file. It can also be useful in a compound chart where different views of the dataset require different transformations.

This second approach – specifying data transformations within the chart specification itself – can be accomplished using the transform_* methods of top-level objects:

Transform

Method

Description

Aggregate Transforms

transform_aggregate()

Create a new data column by aggregating an existing column.

Bin transforms

transform_bin()

Create a new data column by binning an existing column.

Calculate Transform

transform_calculate()

Create a new data column using an arithmetic calculation on an existing column.

Density Transform

transform_density()

Create a new data column with the kernel density estimate of the input.

Filter Transform

transform_filter()

Select a subset of data based on a condition.

Flatten Transform

transform_flatten()

Flatten array data into columns.

Fold Transform

transform_fold()

Convert wide-form data into long-form data (opposite of pivot).

Impute Transform

transform_impute()

Impute missing data.

Join Aggregate Transform

transform_joinaggregate()

Aggregate transform joined to original data.

LOESS Transform

transform_loess()

Create a new column with LOESS smoothing of data.

Lookup Transform

transform_lookup()

One-sided join of two datasets based on a lookup key.

Pivot Transform

transform_pivot()

Convert long-form data into wide-form data (opposite of fold).

Quantile Transform

transform_quantile()

Compute empirical quantiles of a dataset.

Regression Transform

transform_regression()

Fit a regression model to a dataset.

Sample Transform

transform_sample()

Random sub-sample of the rows in the dataset.

Stack Transform

transform_stack()

Compute stacked version of values.

TimeUnit Transform

transform_timeunit()

Discretize/group a date by a time unit (day, month, year, etc.)

Window Transform

transform_window()

Compute a windowed aggregation