mkt.databases.utils.add_one_hot_encoding_to_dataframe
- mkt.databases.utils.add_one_hot_encoding_to_dataframe(df: DataFrame, col_name: str | list[str], prefix: str | list[str] | None = None, bool_drop: bool = True, col_drop: str | list[str] | None = None) DataFrame[source]
Add one-hot encoding for one or more specified columns in a DataFrame.
- Parameters:
df (pd.DataFrame) – Input DataFrame.
col_name (str | list[str]) – Column name(s) to apply one-hot encoding.
prefix (str | list[str] | None, optional) – Prefix(es) for the new columns. If None, uses column names as prefixes. If list, must match length of col_name, by default None.
bool_drop (bool, optional) – If True, drop the original column(s) after encoding, by default True.
col_drop (str | list[str] | None, optional) – If specified, drop these column(s) after encoding (i.e., to avoid multicollinearity).
- Returns:
DataFrame with one-hot encoded columns added.
- Return type:
pd.DataFrame