Skip to content

by_type

champalimaud.by_type

Collect the values of a table column for each cell type.

values_by_type(table, column, types)

The values of a column among the rows of each type.

Parameters:

Name Type Description Default
table DataFrame

Has a column type and the column named by column.

required
column str

Column whose values are collected.

required
types sequence of str

The types to collect, in order.

required

Returns:

Type Description
dict of str to polars.Series

One series per type; a type with no row has an empty series.

See Also

champalimaud.overlap.sets_by_type : The same, as sets.

Examples:

>>> import polars as pl
>>> table = pl.DataFrame({"type": ["P", "P", "Q"], "s": [3, 5, 4]})
>>> by_type = values_by_type(table, "s", ["P", "Q"])
>>> {t: values.to_list() for t, values in by_type.items()}
{'P': [3, 5], 'Q': [4]}
Source code in champalimaud/by_type.py
def values_by_type(
    table: pl.DataFrame, column: str, types: Sequence[str]
) -> dict[str, pl.Series]:
    """The values of a column among the rows of each type.

    Parameters
    ----------
    table : polars.DataFrame
        Has a column ``type`` and the column named by `column`.
    column : str
        Column whose values are collected.
    types : sequence of str
        The types to collect, in order.

    Returns
    -------
    dict of str to polars.Series
        One series per type; a type with no row has an empty series.

    See Also
    --------
    champalimaud.overlap.sets_by_type : The same, as sets.

    Examples
    --------
    >>> import polars as pl
    >>> table = pl.DataFrame({"type": ["P", "P", "Q"], "s": [3, 5, 4]})
    >>> by_type = values_by_type(table, "s", ["P", "Q"])
    >>> {t: values.to_list() for t, values in by_type.items()}
    {'P': [3, 5], 'Q': [4]}
    """
    return {t: table.filter(pl.col("type") == t)[column] for t in types}