1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
.. currentmodule:: altair
.. _user-guide-flatten-transform:
Flatten
~~~~~~~
The flatten transform can be used to extract the contents of arrays from data entries.
This will not generally be useful for well-structured data within pandas dataframes,
but it can be useful for working with data from other sources.
As an example, consider this dataset which uses a common convention in JSON data,
a set of fields each containing a list of entries:
.. altair-plot::
:output: none
import numpy as np
rand = np.random.RandomState(0)
def generate_data(N):
mean = rand.randn()
std = rand.rand()
return list(rand.normal(mean, std, N))
data = [
{'label': 'A', 'values': generate_data(20)},
{'label': 'B', 'values': generate_data(30)},
{'label': 'C', 'values': generate_data(40)},
{'label': 'D', 'values': generate_data(50)},
]
This kind of data structure does not work well in the context of dataframe
representations, as we can see by loading this into pandas:
.. altair-plot::
:output: repr
import pandas as pd
df = pd.DataFrame.from_records(data)
df
Alair's flatten transform allows you to extract the contents of these arrays
into a column that can be referenced by an encoding:
.. altair-plot::
import altair as alt
alt.Chart(df).transform_flatten(
['values']
).mark_tick().encode(
x='values:Q',
y='label:N',
)
This can be particularly useful in cleaning up data specified via a JSON URL,
without having to first load the data for manipulation in pandas.
Transform Options
^^^^^^^^^^^^^^^^^
The :meth:`~Chart.transform_flatten` method is built on the :class:`~FlattenTransform`
class, which has the following options:
.. altair-object-table:: altair.FlattenTransform
|