Convert multiple boolean columns which names start with string `abc_` at once into integer dtype
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Track title: CC E Schuberts Piano Sonata D 784 in A
--
Chapters
00:00 Question
00:28 Accepted answer (Score 6)
00:43 Answer 2 (Score 7)
01:16 Answer 3 (Score 5)
01:31 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
--
Track title: CC E Schuberts Piano Sonata D 784 in A
--
Chapters
00:00 Question
00:28 Accepted answer (Score 6)
00:43 Answer 2 (Score 7)
01:16 Answer 3 (Score 5)
01:31 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)