Skip to main content

Python Pandas: How to Map True/False to 1/0 in a Pandas DataFrame

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 True

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")

# Convert the entire DataFrame
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: object

To 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 True
tip

astype(int) is the fastest and most Pythonic method for this conversion. It leverages NumPy's vectorized type casting under the hood, making it significantly faster than element-wise approaches like apply() or applymap().

Method 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')

# Replace True with 1 and False with 0
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 1
note

This method is useful when you need to replace specific values selectively rather than casting an entire column's type.

Method 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')

# Map True/False to 1/0 for each column
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 1
map() introduces NaN for unmapped values

If a column contains values not present in the mapping dictionary, map() replaces them with NaN:

# ❌ Column has values not in the dictionary
df['Column1'] = df['Column1'].map({True: 1})
# False values become NaN!

Fix: Always include all possible values in the mapping dictionary, or use replace() instead, which leaves unmapped values unchanged.

Method 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')

# Apply lambda to each column, then to each element
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 1

This 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')

# Apply lambda to every element
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 1
Deprecation notice for applymap()

Starting with Pandas 2.1.0, applymap() has been deprecated in favor of map() (which now works on DataFrames, not just Series). If you are using Pandas 2.1+, use:

# Pandas 2.1+
df = df.map(lambda x: 1 if x else 0)

Comparison of Methods

MethodScopePerformanceBest For
astype(int)Entire DataFrame or columnFastest (vectorized)Simple Boolean-to-integer conversion
replace()Entire DataFrame or columnFastExplicit value mapping, mixed data
map() (Series)Single columnFastColumn-specific mapping with a dictionary
apply() + lambdaColumn or row-wiseSlowerCustom transformation logic
applymap() / map()Element-wiseSlowerElement-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')

# Select only Boolean columns and convert them
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 1

Using 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'] * 1

While 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() or applymap() 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.