Ib D
Ib D

Reputation: 441

Label encoding without NAN Value

I would like to encode categorical variables without encoding the missing values. For the moment, I could not find the right solution, here is my code:


# To define my df :
df = pd.DataFrame({'A': ['X', np.NaN, 'Z'], 'B': ['DB', 'AB', 'CA'], 'C': ['KH', 1, np.NaN]})
df :

A   B   C
0   X   DB  KH
1   NaN AB  1
2   Z   CA  NaN
# To encoding juste A variable :
Le = preprocessing.LabelEncoder()
target = Le.fit_transform(df['A'].astype(str))

# but this method also encodes NAN values

# then I tried another handle but it does not work:

Le = preprocessing.LabelEncoder()

# define the values of A not null and try again labelencoding:

Anotnull = df.loc[df['A'] != np.nan]
target = Le.fit_transform(Anotnull.astype(str))

The objective is to make labelencoding without touching the NaN values

Upvotes: 4

Views: 3012

Answers (1)

Scriddie
Scriddie

Reputation: 3781

So this is not technically label encoding "without touching the nans" but it will leave you with a label encoded data frame with the nans in their original place.

import pandas as pd
from sklearn.preprocessing import LabelEncoder


df_raw = pd.DataFrame({"feature1": ["a", "b", "c", np.nan, "e"],
                       "feature2": ["h", "i", np.nan, "k", "l"]})

# 1st possibility
df_temp = df_raw.astype("str").apply(LabelEncoder().fit_transform)
df_final = df_temp.where(~df_raw.isna(), df_raw)

# 2nd possibility
df_temp = df_raw.astype("category").apply(lambda x: x.cat.codes)
df_final = df_temp.where(~df_raw.isna(), df_raw)

Upvotes: 5

Related Questions