Convert Boolean Column to Integer in pandas DataFrame in Python (3 Examples)

In this Python programming tutorial you’ll learn how to convert a True/False boolean data type to a 1/0 integer dummy in a pandas DataFrame column.

Setting up the Examples

import pandas as pd                                                     # Import pandas library
df = pd.DataFrame({'A':[True, False, False, True, False, False, True],  # Constructing a pandas DataFrame
                   'B':[False, True, False, True, True, False, False],
                   'C':[True, True, False, True, False, False, True]})
print(df)
#        A      B      C
# 0   True  False   True
# 1  False   True   True
# 2  False  False  False
# 3   True   True   True
# 4  False   True  False
# 5  False  False  False
# 6   True  False   True
print(df.dtypes)                                                        # Printing the data types of all columns
# A    bool
# B    bool
# C    bool
# dtype: object

Example 1: Transforming One Column of a pandas DataFrame from Boolean to Integer

df1 = df.copy()                                                         # Duplicate pandas DataFrame
df1['A'] = df1['A'].astype(int)                                         # Converting boolean to integer
print(df1)                                                              # Display updated pandas DataFrame
#    A      B      C
# 0  1  False   True
# 1  0   True   True
# 2  0  False  False
# 3  1   True   True
# 4  0   True  False
# 5  0  False  False
# 6  1  False   True
print(df1.dtypes)                                                       # Printing the data types of all columns
# A    int32
# B     bool
# C     bool
# dtype: object

Example 2: Transforming Multiple Columns of a pandas DataFrame from Boolean to Integer

df2 = df.copy()                                                         # Duplicate pandas DataFrame
df2 = df2.astype({'A': int, 'C': int})                                  # Converting boolean to integer
print(df2)                                                              # Display updated pandas DataFrame
#    A      B  C
# 0  1  False  1
# 1  0   True  1
# 2  0  False  0
# 3  1   True  1
# 4  0   True  0
# 5  0  False  0
# 6  1  False  1
print(df2.dtypes)                                                       # Printing the data types of all columns
# A    int32
# B     bool
# C    int32
# dtype: object

Example 3: Transforming Each Column of a pandas DataFrame from Boolean to Integer

df3 = df.copy()                                                         # Duplicate pandas DataFrame
df3 = df3.astype(int)                                                   # Converting boolean to integer
print(df3)                                                              # Display updated pandas DataFrame
#    A  B  C
# 0  1  0  1
# 1  0  1  1
# 2  0  0  0
# 3  1  1  1
# 4  0  1  0
# 5  0  0  0
# 6  1  0  1
print(df3.dtypes)                                                       # Printing the data types of all columns
# A    int32
# B    int32
# C    int32
# dtype: object

Further Resources & Related Tutorials

You may find some related Python programming tutorials on topics such as data conversion, groups, counting, and lists below.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top