Can use pd.get_dummies with drop_first=True credit to @piRSquared
pd.get_dummies(df, drop_first=True)
# a_y b_t c_unknown d_not found
#0 1 1 0 0
#1 0 0 1 1
If this needs to be done for only binary object columns subset first.
df = pd.DataFrame({'a': ['y', 'n', 'c'],
'b': ['t', 'f', 't'],
'c': ['known', 'unknown', 'known'],
'd': ['found', 'not found', 'found'],
'e': [1, 2, 2]})
pd.get_dummies(df.loc[:, df.agg('nunique') == 2].select_dtypes(include='object'),
drop_first=True)
# b_t c_unknown d_not found
#0 1 0 0
#1 0 1 1
#2 1 0 0
If there are a small number of binary responses across columns, consider creating a dictionary and mapping the values:
d = {'y': 1, 'n': 0,
't': 1, 'f': 0,
'known': 1, 'unknown': 0,
'found': 1, 'not found': 0}
s = (df.agg('nunique') == 2) & (df.dtypes == 'object')
for col in s[s].index:
df[col] = df[col].map(d)
# a b c d e
#0 y 1 1 1 1
#1 n 0 0 0 2
#2 c 1 1 1 2
# |
# `a` not mapped because trinary
astype('category')?pd.get_dummies(df).iloc[:, ::2]. Otherwise please provide a more complete example and explanation of what you need.df.assign(**df.select_dtypes(object).apply(lambda c: c.factorize()[0]))nunique == 2?