dtoolkit.transformer.FilterInTF#

class dtoolkit.transformer.FilterInTF(*args, **kwargs)[source]#

A transformer could filter DataFrame contents.

See also

dtoolkit.accessor.dataframe.filter_in

This transformer’s prototype method.

Examples

>>> from dtoolkit.transformer import FilterInTF
>>> import pandas as pd
>>> df = pd.DataFrame(
...     {
...         'legs': [2, 4, 2],
...         'wings': [2, 0, 0],
...     },
...     index=['falcon', 'dog', 'cat'],
... )
>>> df
        legs  wings
falcon     2      2
dog        4      0
cat        2      0

When condition is a list check whether every value in the DataFrame is present in the list (which animals have 0 or 2 legs or wings).

Filter rows.

>>> tf = FilterInTF([0, 2])
>>> tf.transform(df)
        legs  wings
falcon     2      2
cat        2      0

When condition is a dict, we can pass values to check for each column separately:

>>> tf = FilterInTF({'legs': [2], 'wings': [2]})
>>> tf.transform(df)
        legs  wings
falcon     2      2

When values is a Series or DataFrame the index and column must match. Note that falcon does not match based on the number of legs in df2.

>>> other = pd.DataFrame({'legs': [8, 2], 'wings': [0, 2]},
...                      index=['spider', 'falcon'])
>>> other
        legs  wings
spider     8      0
falcon     2      2
>>> tf = FilterInTF(other)
>>> tf.transform(df)
        legs  wings
falcon     2      2
Attributes:
inverse_transform_method

Methods

fit(*_)

Fit transformer.

fit_transform(X[, y])

Fit to data, then transform it.

inverse_transform(X)

Undo transform to X.

set_output(*[, transform])

Set output container.

transform(X)

Transform X.

transform_method(df, condition, /[, how, ...])

Filter DataFrame contents.

update_invargs(*args, **kwargs)

Inverse transform method argument entry.