Option 1: column conversion all boolean ( dtype == 'bool'
)
df.loc[:, df.dtypes.eq('bool')] = df.loc[:, df.dtypes.eq('bool')].astype(np.int8)
Option 2: if you want to convert only those logical columns that start with abc_
:
col_mask = df.dtypes.eq('bool') & df.columns.str.contains('^abc_')
df.loc[:, col_mask] = df.loc[:, col_mask].astype(np.int8)
Option 3: Convert by Column Names Only
df.loc[:, df.columns.str.match(r'^abc_.*$')] = \
df.filter(regex=r'^abc_').astype(np.int8)
source
share