Convert multiple boolean columns which names start with string `abc_` at once into integer dtype
--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Puddle Jumping Looping
--
Chapters
00:00 Convert Multiple Boolean Columns Which Names Start With String `Abc_` At Once Into Integer Dtype
00:22 Answer 1 Score 7
00:49 Answer 2 Score 5
01:00 Accepted Answer Score 6
01:10 Thank you
--
Full question
https://stackoverflow.com/questions/4827...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #pandas #dataframe
#avk47
    Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Puddle Jumping Looping
--
Chapters
00:00 Convert Multiple Boolean Columns Which Names Start With String `Abc_` At Once Into Integer Dtype
00:22 Answer 1 Score 7
00:49 Answer 2 Score 5
01:00 Accepted Answer Score 6
01:10 Thank you
--
Full question
https://stackoverflow.com/questions/4827...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #pandas #dataframe
#avk47
ANSWER 1
Score 7
Option 1: converting all boolean (dtype == 'bool') columns
df.loc[:, df.dtypes.eq('bool')] = df.loc[:, df.dtypes.eq('bool')].astype(np.int8)
Option 2: if only those boolean columns that start with abc_ should be converted:
col_mask = df.dtypes.eq('bool') & df.columns.str.contains('^abc_')
df.loc[:, col_mask] = df.loc[:, col_mask].astype(np.int8)
Option 3: converting only by column names
df.loc[:, df.columns.str.match(r'^abc_.*$')] = \
    df.filter(regex=r'^abc_').astype(np.int8)
ACCEPTED ANSWER
Score 6
You can do this with filter and an in-place update.
df.update(df.filter(regex='^abc_').astype(int))
ANSWER 3
Score 5
By using str.contains
df.loc[:,df.columns.str.contains('abc_')]=df.loc[:,df.columns.str.contains('abc_')].astype(int)