0

I have a dataset with 39 categorical and 27 numerical features. I am trying to encode the categorical data and need to be able to inverse transform and call transform for each column again. Is there a prettier way of doing it than defining 39 separate LabelEncoder instances, and then fit_transform to each column individually?

I feel like I am missing something obvious, but I cant figure it out!

enc = LabelEncoder
cat_feat = [col for col in input_df2.columns if input_df2[col].dtype == 'object']
cat_feat = np.asarray(cat_feat)

le1 =LabelEncoder()
le2 =LabelEncoder()
le3 =LabelEncoder()
...
#extended to le39

def label(input):
       input.iloc[:, 1] = le1.fit_transform(input.iloc[:, 1])
       input.iloc[:, 3] = le1.fit_transform(input.iloc[:, 3])
       input.iloc[:, 4] = le1.fit_transform(input.iloc[:, 4])
       ... 
       return input
Tom_Scott
  • 95
  • 7

1 Answers1

1

DataFrame.apply is just for this. It will call the specified function for each column of the dataframe (or each row, if you pass it axis=1):

encoders = []

def apply_label_encoder(col):
    le = LabelEncoder()
    encoders.append(le)
    le.fit_transform(col)
    return 

input_df.iloc[:, 1:] = input_df.iloc[:, 1:].apply(apply_label_encoder)
  • Thank you - I have not used apply too much. Would I still need to define individual encoders for each column? – Tom_Scott Mar 10 '22 at 21:42
  • Oh, sorry! I didn't understand fully. Wait a moment :) –  Mar 10 '22 at 21:43
  • Check the answer now. If you want to access the encoder for a particular column, e.g. the 1st just use `encoder[0]` (zero-based) –  Mar 10 '22 at 21:47