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)
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.
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().
You can use df. astype() with a dictionary for the columns you want to change with the corresponding dtype.
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.
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)
You can do this with filter
and an in-place update
.
df.update(df.filter(regex='^abc_').astype(int))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With