Converting Boolean values (True/False) to integers (1/0) in a Pandas DataFrame is a common data preprocessing step. Many machine learning algorithms, statistical functions, and data export formats require numeric representations rather than Boolean types. Python treats True as 1 and False as 0 internally, but explicitly converting them ensures compatibility, clarity, and prevents unexpected behavior in calculations.
In this guide, you will learn five different methods to perform this conversion, understand when each is most appropriate, and see the output for every approach.
Setting Up the Example DataFrame
All examples below use the same starting DataFrame:
import pandas as pd
data = {
'Column1': [True, False, True, False],
'Column2': [False, True, False, True]
}
df = pd.DataFrame(data)
print(df)Output:
Column1 Column2
0 True False
1 False True
2 True False
3 False TrueMethod 1: Using astype(int) - The Recommended Approach
The simplest and most efficient way to convert Boolean values to integers is using astype(int). This directly casts the data type of the entire DataFrame or specific columns:
import pandas as pd
data = {
'Column1': [True, False, True, False],
'Column2': [False, True, False, True]
}
df = pd.DataFrame(data)
print("Before conversion:")
print(df)
print(f"Dtypes:\n{df.dtypes}\n")
df = df.astype(int)
print("After conversion:")
print(df)
print(f"Dtypes:\n{df.dtypes}")Output:
Before conversion:
Column1 Column2
0 True False
1 False True
2 True False
3 False True
Dtypes:
Column1 bool
Column2 bool
dtype: object
After conversion:
Column1 Column2
0 1 0
1 0 1
2 1 0
3 0 1
Dtypes:
Column1 int64
Column2 int64
dtype: objectTo convert only specific columns:
import pandas as pd
data = {
'Column1': [True, False, True, False],
'Column2': [False, True, False, True]
}
df = pd.DataFrame(data)
df['Column1'] = df['Column1'].astype(int)
print(df)Output:
Column1 Column2
0 1 False
1 0 True
2 1 False
3 0 TrueMethod 2: Using replace()
The replace() method lets you map specific values to new ones using a dictionary. This approach is explicit about what is being changed:
import pandas as pd
data = {
'Column1': [True, False, True, False],
'Column2': [False, True, False, True]
}
df = pd.DataFrame(data)
print("Original:")
print(df, '\n')
df = df.replace({True: 1, False: 0})
print("After replace:")
print(df)Output:
Original:
Column1 Column2
0 True False
1 False True
2 True False
3 False True
After replace:
Column1 Column2
0 1 0
1 0 1
2 1 0
3 0 1Method 3: Using map() on Individual Columns
The map() method works on a single Series (column) and applies a mapping dictionary to each element:
import pandas as pd
data = {
'Column1': [True, False, True, False],
'Column2': [False, True, False, True]
}
df = pd.DataFrame(data)
print("Original:")
print(df, '\n')
df['Column1'] = df['Column1'].map({True: 1, False: 0})
df['Column2'] = df['Column2'].map({True: 1, False: 0})
print("After map:")
print(df)Output:
Original:
Column1 Column2
0 True False
1 False True
2 True False
3 False True
After map:
Column1 Column2
0 1 0
1 0 1
2 1 0
3 0 1Method 4: Using apply() With a Lambda Function
The apply() method applies a function along an axis of the DataFrame. Combined with a lambda, it can convert values element by element:
import pandas as pd
data = {
'Column1': [True, False, True, False],
'Column2': [False, True, False, True]
}
df = pd.DataFrame(data)
print("Original:")
print(df, '\n')
df_converted = df.apply(lambda col: col.apply(lambda val: 1 if val else 0))
print("After apply:")
print(df_converted)Output:
Original:
Column1 Column2
0 True False
1 False True
2 True False
3 False True
After apply:
Column1 Column2
0 1 0
1 0 1
2 1 0
3 0 1This method is flexible but verbose. It is best suited for custom transformation logic that goes beyond simple type conversion.
Method 5: Using applymap() for Element-Wise Transformation
The applymap() method applies a function to every individual element in the DataFrame:
import pandas as pd
data = {
'Column1': [True, False, True, False],
'Column2': [False, True, False, True]
}
df = pd.DataFrame(data)
print("Original:")
print(df, '\n')
df = df.applymap(lambda x: 1 if x else 0)
print("After applymap:")
print(df)Output:
Original:
Column1 Column2
0 True False
1 False True
2 True False
3 False True
After applymap:
Column1 Column2
0 1 0
1 0 1
2 1 0
3 0 1Comparison of Methods
| Method | Scope | Performance | Best For |
|---|---|---|---|
astype(int) |
Entire DataFrame or column | Fastest (vectorized) | Simple Boolean-to-integer conversion |
replace() |
Entire DataFrame or column | Fast | Explicit value mapping, mixed data |
map() (Series) |
Single column | Fast | Column-specific mapping with a dictionary |
apply() + lambda |
Column or row-wise | Slower | Custom transformation logic |
applymap() / map() |
Element-wise | Slower | Element-level custom logic |
Handling DataFrames With Mixed Column Types
When your DataFrame contains both Boolean and non-Boolean columns, convert only the Boolean columns to avoid errors:
import pandas as pd
data = {
'Name': ['Alice', 'Bob', 'Charlie', 'Diana'],
'Active': [True, False, True, False],
'Premium': [False, True, False, True]
}
df = pd.DataFrame(data)
print("Original:")
print(df, '\n')
bool_cols = df.select_dtypes(include='bool').columns
df[bool_cols] = df[bool_cols].astype(int)
print("After conversion:")
print(df)Output:
Original:
Name Active Premium
0 Alice True False
1 Bob False True
2 Charlie True False
3 Diana False True
After conversion:
Name Active Premium
0 Alice 1 0
1 Bob 0 1
2 Charlie 1 0
3 Diana 0 1Using select_dtypes(include='bool') ensures that only Boolean columns are converted, leaving string and numeric columns untouched.
Multiplying by 1 - A Quick Trick
Since Python treats True as 1 and False as 0, multiplying a Boolean column by 1 converts it to integers:
df['Column1'] = df['Column1'] * 1While this works, it is less readable and explicit than astype(int). Use it only for quick, one-off conversions.
Conclusion
Converting True/False to 1/0 in a Pandas DataFrame is straightforward with multiple methods available.
astype(int)is the recommended approach for most cases: it is the fastest, most readable, and most Pythonic.- Use
replace()when you need explicit value-to-value mapping - Use
map()for column-specific dictionary-based transformations - Use
apply()orapplymap()only when your conversion logic is more complex than a simple type cast. - For DataFrames with mixed column types, use
select_dtypes(include='bool')to target only the Boolean columns.