Table of Contents
Pandas provides a way to convert boolean values to integer values through the use of the .astype() method. This method takes a boolean Series or DataFrame and converts it to a numerical representation, where True is represented as 1 and False is represented as 0. This conversion is useful for performing mathematical operations on boolean data, such as in a conditional statement. This conversion can also help in data cleaning tasks, as the boolean values can be used as a flag to identify and remove invalid data.
You can use the following basic syntax to convert a column of boolean values to a column of integer values in pandas:
df.column1 = df.column1.replace({True: 1, False: 0})
The following example shows how to use this syntax in practice.
Example: Convert Boolean to Integer in Pandas
Suppose we have the following pandas DataFrame:
import pandas as pd #create DataFrame df = pd.DataFrame({'team': ['A', 'B', 'C', 'D', 'E', 'F', 'G'], 'points': [18, 22, 19, 14, 14, 11, 20], 'playoffs': [True, False, False, False, True, False, True]}) #view DataFrame df
We can use to quickly check the data type of each column:
#check data type of each column
df.dtypes
team object
points int64
playoffs bool
dtype: object
We can see that the ‘playoffs’ column is of type boolean.
We can use the following code to quickly convert the True/False values in the ‘playoffs’ column into 1/0 integer values:
#convert 'playoffs' column to integer df.playoffs = df.playoffs.replace({True: 1, False: 0}) #view updated DataFrame df team points playoffs 0 A 18 1 1 B 22 0 2 C 19 0 3 D 14 0 4 E 14 1 5 F 11 0 6 G 20 1
Each True value was converted to 1 and each False value was converted to 0.
We can use dtypes again to verify that the ‘playoffs’ column is now an integer:
#check data type of each column df.dtypes team object points int64 playoffs int64 dtype: object
We can see that the ‘playoffs’ column is now of type int64.