Skip to content

Instantly share code, notes, and snippets.

@juliussin
Created November 9, 2022 09:07
Show Gist options
  • Save juliussin/bbdb8895b302d17a001c2f18eb518db0 to your computer and use it in GitHub Desktop.
Save juliussin/bbdb8895b302d17a001c2f18eb518db0 to your computer and use it in GitHub Desktop.
from typing import List
import pandas as pd
def one_hot_to_categorical(df: pd.DataFrame, one_hot_features: List[str], new_feature: str, rm_ori: bool = False) -> pd.DataFrame:
"""
One Hot Encoding to Categorical Feature
"""
return_df = df.copy()
one_hot_df = return_df.loc[:, one_hot_features].astype(bool)
for row in range(len(one_hot_df)):
temp_val = 0
for n, val in enumerate(one_hot_df.iloc[row, :].to_list()):
temp_val += 2**n * val
return_df.loc[row, new_feature] = temp_val
return_df[[new_feature]] = return_df[[new_feature]].astype(int)
if rm_ori:
return_df = return_df.drop(columns=one_hot_features)
return return_df
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment