In [1]:
import pandas as pd
from sklearn.preprocessing import LabelEncoder
In [2]:
df = pd.DataFrame({'col_1': [1, 0, 1, None],
'col_2': [1.2, 3.1, 4.4, 5.5],
'col_3': [1, 2, 3, 4],
'col_4': ['a', 'b', 'c', 'd']})
In [3]:
df
Out[3]:
In [4]:
df.info()
In [5]:
bool_cols = [col for col in df if len(df[[col]].dropna()[col].unique()) == 2]
In [6]:
for col in bool_cols:
label = LabelEncoder()
label.fit(list(df[col].values.astype("str")))
df[col] = label.transform(list(df[col].values.astype("str")))
In [7]:
df.info()
In [8]:
df
Out[8]:
In [ ]: