Pandas DataFrame中将True/False映射到1/0

发布时间:2024年01月06日

在本文中,我们将看到如何在Pandas DataFrame中将True/False映射到1/0。True/False到1/0的转换在执行计算时至关重要,并且可以轻松分析数据。

1. replace方法

在这个例子中,我们使用Pandas replace()方法将True/False映射到1/0。在这里,我们创建了一个名为df的示例DataFrame,其中有两个列,‘Column 1’和’Column 2’,包含布尔值。然后,我们在DataFrame df上使用.replace()方法。我们提供了一个字典,其中我们指定True应该被替换为1,False应该被替换为0。

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

在这里插入图片描述

2. applymap函数

在这个例子中,我们使用Pandas applymap()。在这里,创建包含布尔值的示例DataFrame df。然后,我们在DataFrame df上使用.applymap()函数。lambda函数lambda x:1 if x else 0按元素应用于DataFrame中的每个值。它检查x值是否为True,如果是,则返回1;否则返回0。

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

在这里插入图片描述

3. astype方法

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

在这里插入图片描述

4. apply和lambda函数

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

在这里插入图片描述

5. map方法

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

在这里插入图片描述

文章来源:https://blog.csdn.net/qq_42034590/article/details/134694785
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。