class feature_engine.encoding.CountFrequencyEncoder(encoding_method='count', variables=None, ignore_format=False, errors='ignore')[source]#

The CountFrequencyEncoder() replaces categories by either the count or the percentage of observations per category.

For example in the variable colour, if 10 observations are blue, blue will be replaced by 10. Alternatively, if 10% of the observations are blue, blue will be replaced by 0.1.

The CountFrequencyEncoder() will encode only categorical variables by default (type ‘object’ or ‘categorical’). You can pass a list of variables to encode. Alternatively, the encoder will find and encode all categorical variables (type ‘object’ or ‘categorical’).

With ignore_format=True you have the option to encode numerical variables as well. The procedure is identical, you can either enter the list of variables to encode, or the transformer will automatically select all variables.

The encoder first maps the categories to the counts or frequencies for each variable (fit). The encoder then replaces the categories with those numbers (transform).

More details in the User Guide.

encoding_method: str, default=’count’

Desired method of encoding.

‘count’: number of observations per category

‘frequency’: percentage of observations per category

variables: list, default=None

The list of categorical variables that will be encoded. If None, the encoder will find and transform all variables of type object or categorical by default. You can also make the transformer accept numerical variables, see the next parameter.

ignore_format: bool, default=False

Whether the format in which the categorical variables are cast should be ignored. If False, the encoder will automatically select variables of type object or categorical, or check that the variables entered by the user are of type object or categorical. If True, the encoder will select all variables or accept all variables entered by the user, including those cast as numeric.

errors: string, default=’ignore’

Indicates what to do when categories not present in the train set are encountered during transform. If ‘raise’, then rare categories will raise an error. If ‘ignore’, then rare categories will be set as NaN and a warning will be raised instead.


Dictionary with the count or frequency per category, per variable.


The group of variables that will be transformed.


The number of features in the train set used in fit.

See also



NAN are introduced when encoding categories that were not present in the training dataset. If this happens, try grouping infrequent categories using the RareLabelEncoder().

There is a similar implementation in the the open-source package Category encoders



Learn the count or frequency per category, per variable.


Encode the categories to numbers.


Fit to the data, then transform it.


Encode the numbers into the original categories.

fit(X, y=None)[source]#

Learn the counts or frequencies which will be used to replace the categories.

X: pandas dataframe of shape = [n_samples, n_features]

The training dataset. Can be the entire dataframe, not just the variables to be transformed.

y: pandas Series, default = None

y is not needed in this encoder. You can pass y or None.

fit_transform(X, y=None, **fit_params)[source]#

Fit to data, then transform it.

Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.

Xarray-like of shape (n_samples, n_features)

Input samples.

yarray-like of shape (n_samples,) or (n_samples, n_outputs), default=None

Target values (None for unsupervised transformations).


Additional fit parameters.

X_newndarray array of shape (n_samples, n_features_new)

Transformed array.


Get parameters for this estimator.

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.


Parameter names mapped to their values.


Convert the encoded variable back to the original values.

X: pandas dataframe of shape = [n_samples, n_features].

The transformed dataframe.

X_tr: pandas dataframe of shape = [n_samples, n_features].

The un-transformed dataframe, with the categorical variables containing the original values.


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.


Estimator parameters.

selfestimator instance

Estimator instance.


Replace categories with the learned parameters.

X: pandas dataframe of shape = [n_samples, n_features].

The dataset to transform.

X_new: pandas dataframe of shape = [n_samples, n_features].

The dataframe containing the categories replaced by numbers.