AggJoiner#
- class skrub.AggJoiner(aux_table, operations, *, key=None, main_key=None, aux_key=None, cols=None, suffix='')[source]#
Aggregate an auxiliary dataframe before joining it on a base dataframe.
Apply numerical and categorical aggregation operations on the columns (i.e.
cols) to aggregate. See the list of supported operations at the parameteroperations.If
colsis not provided,colsare all columns fromaux_table, exceptaux_key.Accepts
pandas.DataFrameandpolars.DataFrameinputs.Warning
The auxiliary table is stored in memory as part of the state of the transformer, which can lead to high memory usage if the auxiliary table is large.
Additionally, the auxiliary table is frozen in memory after fitting, which means that if the auxiliary table is modified after fitting, the changes will not be reflected in the transformed output. If you need to update the auxiliary table, you will need to refit the transformer.
Consider using the skrub Data Ops and a standard dataframe library (Pandas or Polars) to perform the aggregation instead.
- Parameters:
- aux_tableDataFrameLike or “X”
Auxiliary dataframe to aggregate then join on the base table. The placeholder string “X” can be provided to perform self-aggregation on the input data.
- operations
stror iterable ofstr Aggregation operations to perform on the auxiliary table.
Supported operations are “count”, “mode”, “min”, “max”, “sum”, “median”, “mean”, “std”. The operations “sum”, “median”, “mean”, “std” are reserved to numeric type columns.
- key
str, default=None The column name to use for both
main_keyandaux_keywhen they are the same. Provide eitherkeyor bothmain_keyandaux_key. Ifkeyis an iterable, we will perform a multi-column join.- main_key
stror iterable ofstr, default=None Select the columns from the main table to use as keys during the join operation. If
main_keyis an iterable, we will perform a multi-column join.- aux_key
stror iterable ofstr, default=None Select the columns from the auxiliary dataframe to use as keys during the join operation. If
aux_keyis an iterable, we will perform a multi-column join.- cols
stror iterable ofstr, default=None Select the columns from the auxiliary dataframe to use as values during the aggregation operations. By default,
colsare all columns fromaux_table, exceptaux_key.- suffix
str, default=”” Suffix to append to the
aux_table’s column names. You can use it to avoid duplicate column names in the join.
See also
AggTargetAggregates the target
ybefore joining its aggregation on the base dataframe.JoinerAugments a main table by automatically joining an auxiliary table on it.
MultiAggJoinerExtension of the AggJoiner to multiple auxiliary tables.
Examples
>>> import pandas as pd >>> from skrub import AggJoiner >>> main = pd.DataFrame({ ... "airportId": [1, 2], ... "airportName": ["Paris CDG", "NY JFK"], ... }) >>> aux = pd.DataFrame({ ... "flightId": range(1, 7), ... "from_airport": [1, 1, 1, 2, 2, 2], ... "total_passengers": [90, 120, 100, 70, 80, 90], ... "company": ["DL", "AF", "AF", "DL", "DL", "TR"], ... }) >>> agg_joiner = AggJoiner( ... aux_table=aux, ... operations="mean", ... main_key="airportId", ... aux_key="from_airport", ... cols="total_passengers", ... ) >>> agg_joiner.fit_transform(main) airportId airportName total_passengers_mean 0 1 Paris CDG 103.33... 1 2 NY JFK 80.00...
Methods
fit(X[, y])Aggregate auxiliary table based on the main keys.
fit_transform(X[, y])Aggregate auxiliary table based on the main keys.
Get output feature names for transformation.
get_params([deep])Get parameters for this estimator.
set_output(*[, transform])Set output container.
set_params(**params)Set the parameters of this estimator.
transform(X)Left-join pre-aggregated table on
X.- fit_transform(X, y=None)[source]#
Aggregate auxiliary table based on the main keys.
- Parameters:
- XDataFrameLike
Input data, based table on which to left join the auxiliary table.
- y
None Unused, only here for compatibility.
- Returns:
- DataFrame
The augmented input.
- get_feature_names_out()[source]#
Get output feature names for transformation.
- Returns:
- List of
str Transformed feature names.
- List of
- set_output(*, transform=None)[source]#
Set output container.
Refer to the user guide for more details and Introducing the set_output API for an example on how to use the API.
- Parameters:
- transform{“default”, “pandas”, “polars”}, default=None
Configure output of
transformandfit_transform."default": Default output format of a transformer"pandas": DataFrame output"polars": Polars outputNone: Transform configuration is unchanged
Added in version 1.4:
"polars"option was added.
- Returns:
- selfestimator instance
Estimator instance.
- set_params(**params)[source]#
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as
Pipeline). The latter have parameters of the form<component>__<parameter>so that it’s possible to update each component of a nested object.- Parameters:
- **params
dict Estimator parameters.
- **params
- Returns:
- selfestimator instance
Estimator instance.