janitor.label_encode¶
-
janitor.
label_encode
(df: pandas.core.frame.DataFrame, column_names: Union[str, Iterable[str], Hashable]) → pandas.core.frame.DataFrame[source]¶ Convert labels into numerical data.
This method will create a new column with the string “_enc” appended after the original column’s name. Consider this to be syntactic sugar.
This method behaves differently from encode_categorical. This method creates a new column of numeric data. encode_categorical replaces the dtype of the original column with a “categorical” dtype.
This method mutates the original DataFrame.
Functional usage syntax:
df = label_encode(df, column_names="my_categorical_column") # one way
Method chaining syntax:
import pandas as pd import janitor categorical_cols = ['col1', 'col2', 'col4'] df = pd.DataFrame(...).label_encode(column_names=categorical_cols)
- Parameters
df – The pandas DataFrame object.
column_names – A column name or an iterable (list or tuple) of column names.
- Returns
A pandas DataFrame.