Open In App

Map True/False to 1/0 in a Pandas DataFrame

Last Updated : 29 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see how to map True/False to 1/0 in a Pandas DataFrame. The transformation of True/False to 1/0 is vital when carrying out computations and makes it easy to analyze the data. So, we will see how we can convert the same.

Pandas DataFrame Map True/False to 1/0

Below are the methods by which we can map True/False to 1/0 in Pandas DataFrame:

Python Pandas replace()

In this example, we are using the Pandas replace() method to map True/False to 1/0. Here, we create a sample DataFrame called df with two columns, ‘Column1’ and ‘Column2’, containing Boolean values. Then, we use the .replace() method on the DataFrame df. We provide a dictionary where we specify that True should be replaced with 1 and False should be replaced with 0.

Python3




# Python code to map Boolean values to integer using .replace() method
import pandas as pd
 
# Create a sample DataFrame with two columns, 'Column1'
# and 'Column2', containing Boolean values
data = {'Column1': [True, False, True, False],
        'Column2': [False, True, False, True]}
 
# Create a DataFrame named 'df' using the provided data
df = pd.DataFrame(data)
 
# Print the original DataFrame 'df' containing Boolean values
print(df, '\n')
 
# Use the .replace() method to map True/False to 1/0
df = df.replace({True: 1, False: 0})
 
# Print the updated DataFrame 'df' where Boolean values
# are now represented as integers (1/0)
print(df)


Output

Output

Pandas applymap() function

In this example, we are using Pandas applymap(). Here, e create the sample DataFrame df containing Boolean values. Then, we use the .applymap() function on the DataFrame df. The lambda function lambda x: 1 if x else 0 is applied element-wise to each value in the DataFrame. It checks if the value x is True, and if it is, it returns 1; otherwise, it returns 0.

Python3




# Import the pandas library and alias it as 'pd'
import pandas as pd
 
# Create a sample DataFrame with two columns,
# 'Column1' and 'Column2', containing Boolean values
data = {'Column1': [True, False, True, False],
        'Column2': [False, True, False, True]}
 
# Create a DataFrame named 'df' using the provided data
df = pd.DataFrame(data)
 
# Print the original DataFrame 'df' containing Boolean values
print(df, '\n')
 
# Use .applymap() with a lambda function to map True/False to 1/0
df = df.applymap(lambda x: 1 if x else 0)
 
# Print the updated DataFrame 'df' where Boolean
# values are now represented as integers (1/0)
print(df)


Output

Output

Python Pandas astype() method

In this example, we are using Pandas astype() method to map True/False to 1/0.

Python3




# Import the pandas library and alias it as 'pd'
import pandas as pd
 
# Create a sample DataFrame with two columns, 'Column1'
# and 'Column2', containing Boolean values
data = {'Column1': [True, False, True, False],
        'Column2': [False, True, False, True]}
 
# Create a DataFrame named 'df' using the provided data
df = pd.DataFrame(data)
 
# Print the original DataFrame 'df' containing Boolean values
print(df,'/n')
 
# Convert the 'Column1' and 'Column2' columns from
# Boolean (True/False) to integers (1/0)
df['Column1'] = df['Column1'].astype(int)
df['Column2'] = df['Column2'].astype(int)
 
# Print the updated DataFrame 'df' where Boolean
# values are now represented as integers
print(df)


Output

Output

Pandas apply() with lambda function

In this example, we are using Pandas apply() method to map Boolean values to 1/0 in a Pandas DataFrame. Here, we are creating a sample DataFrame named ‘df’ with two columns, ‘Column1’ and ‘Column2’, both containing Boolean values. The code then employs the apply() method along with a lambda function. The lambda function checks each element in the DataFrame and returns 1 if it’s True and 0 if it’s False. This mapping is applied to both ‘Column1’ and ‘Column2’ using .apply(), effectively converting all True values to 1 and all False values to 0. The result is stored in ‘df_apply,’ and the code prints this updated DataFrame.

Python3




# Import the pandas library and alias it as 'pd'
import pandas as pd
 
# Create a sample DataFrame with two columns, 'Column1'
# and 'Column2', containing Boolean values
data = {'Column1': [True, False, True, False],
        'Column2': [False, True, False, True]}
 
# Create a DataFrame named 'df' using the provided data
df = pd.DataFrame(data)
print(df,"\n")
 
# We define a lambda function that converts True to 1 and False
# to 0 and apply it to each column using .apply()
df_apply = df.apply(lambda x: x.apply(lambda y: 1 if y else 0))
 
# Print the DataFrame 'df_apply' with the mapping applied
# using .apply() and a lambda function
print("\nUsing .apply() method with lambda function:")
print(df_apply)


Output

apply-lambda

Python Pandas map()

In this example, we are using Pandas map() method to map Boolean values to 1/0 in a Pandas DataFrame. Here, we are creating a sample DataFrame named ‘df’ with two columns, ‘Column1’ and ‘Column2,’ both containing Boolean values. The original DataFrame is then printed to display its initial contents. To perform the mapping, we use the map() method on ‘Column1’ and provide a dictionary that specifies the mapping: True is mapped to 1, and False is mapped to 0. This operation directly converts all True values in ‘Column 1 to 1 and all False values to 0. The updated ‘Column1’ in the original DataFrame ‘df’ reflects this transformation.

Python3




# Import the pandas library and alias it as 'pd'
import pandas as pd
 
# Create a sample DataFrame with two columns, 'Column1' and
# 'Column2', containing Boolean values
data = {'Column1': [True, False, True, False],
        'Column2': [False, True, False, True]}
 
# Create a DataFrame named 'df' using the provided data
df = pd.DataFrame(data)
print(df,"\n")
 
# We use .map() on a specific column and provide a dictionary to perform the mapping
df['Column1'] = df['Column1'].map({True: 1, False: 0})
 
# Print the updated 'Column1' in the original DataFrame
# 'df' where Boolean values are mapped to integers
print("\nUsing .map() method for 'Column1':")
print(df)


Output
map-function



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads