Fold Transform

The fold transform is, in short, a way to convert wide-form data to long-form data directly without any preprocessing (see Long-form vs. Wide-form Data for more information). Fold transforms are the opposite of the Pivot Transform.

So, for example, if your data consist of multiple columns that record parallel data for different categories, you can use the fold transform to encode based on those categories:

import numpy as np
import pandas as pd
import altair as alt

rand = np.random.RandomState(0)
data = pd.DataFrame({
    'date': pd.date_range('2019-01-01', freq='D', periods=30),
    'A': rand.randn(30).cumsum(),
    'B': rand.randn(30).cumsum(),
    'C': rand.randn(30).cumsum(),
})

alt.Chart(data).transform_fold(
    ['A', 'B', 'C'],
).mark_line().encode(
    x='date:T',
    y='value:Q',
    color='key:N'
)

Notice here that the fold transform essentially stacks all the values from the specified columns into a single new field named "value", with the associated names in a field named "key".

For an example of the fold transform in action, see Parallel Coordinates Example.

Transform Options

The transform_fold() method is built on the FoldTransform class, which has the following options:

Property

Type

Description

as

array(any)

The output field names for the key and value properties produced by the fold transform. Default value: ["key", "value"]

fold

array(FieldName)

An array of data fields indicating the properties to fold.