Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert multiple boolean columns which names start with string `abc_` at once into integer dtype

I need to have 1 and 0 instead of True and False in a pandas data frame for only columns starting with abc_. Is there any better way of doing this other than my loop:

for col in df:
  if col[:4] =='abc_':
     df[col] = df[col].astype(int) 
like image 258
ibarant Avatar asked Jan 15 '18 21:01

ibarant


People also ask

How do I convert multiple columns to string?

Convert Multiple Columns to String You can also convert multiple columns to string by sending dict of column name -> data type to astype() method. The below example converts column Fee from int to string and Discount from float to string dtype. Yields below output.

How do you change the datatype for multiple columns?

Change Data Type of Multiple Columns in Dataframe To change the data type of multiple columns in the dataframe we are going to use DataFrame. astype().

How do you change the datatype of multiple columns in Python?

You can use df. astype() with a dictionary for the columns you want to change with the corresponding dtype.

How do I convert multiple columns in pandas?

You can use the DataFrame. apply() and pd. to_datetime() function to convert multiple columns to DataTime. apply() function applies a function to each and every row and column of the DataFrame.


2 Answers

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)
like image 139
MaxU - stop WAR against UA Avatar answered Nov 15 '22 04:11

MaxU - stop WAR against UA


You can do this with filter and an in-place update.

df.update(df.filter(regex='^abc_').astype(int))
like image 36
cs95 Avatar answered Nov 15 '22 06:11

cs95