Open In App

Convert Boolean To String In Pandas Dataframe

Last Updated : 06 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Pandas, a powerful data manipulation library in Python, offers multiple ways to convert boolean values to strings within a DataFrame. In this article, we will see how to convert boolean to String in Pandas DataFrame in Python.

Python Convert Boolean To String In Pandas Dataframe

Below are some examples by which we can convert Boolean to String in Pandas DataFrame in Python:

  • Using astype() Method
  • Using apply Function with Lambda
  • Using map() Function
  • Using replace() Method

Boolean to String in Pandas Dataframe Using astype Method

In this example, we leverage the astype method to directly convert boolean values in a DataFrame column to strings. The astype method is a straightforward and concise approach, making it ideal for a quick conversion.

Python3




import pandas as pd
 
# Sample DataFrame
data = {'column_name': [True, False, True, False]}
df = pd.DataFrame(data)
 
print('Before :', df.dtypes)
 
# Convert boolean to string using astype
df['column_name'] = df['column_name'].astype(str)
 
print(df)
 
print('After :', df.dtypes)


Output:

Before : column_name    bool
dtype: object
column_name
0 True
1 False
2 True
3 False
After : column_name object
dtype: object

Convert Boolean to String Using apply Function with Lambda

In this example, we utilize the apply() function with a lambda expression to convert boolean values to strings. This approach offers flexibility and allows for more complex transformations if needed.

Python3




import pandas as pd
 
# Sample DataFrame
data = {'column_name': [True, False, True, False]}
df = pd.DataFrame(data)
 
print('Before :',df.dtypes)
 
# Convert boolean to string using apply and lambda
df['column_name'] = df['column_name'].apply(lambda x: str(x))
 
# Display the DataFrame
print(df)
 
print('After :',df.dtypes)


Output:

Before : column_name    bool
dtype: object
column_name
0 True
1 False
2 True
3 False
After : column_name object
dtype: object

Boolean to String in Pandas Dataframe Using map() Function

In this example, the map() function is employed along with a predefined dictionary to map boolean values to their corresponding strings. This approach is useful when a custom mapping is required.

Python3




import pandas as pd
 
# Sample DataFrame
data = {'column_name': [True, False, True, False]}
df = pd.DataFrame(data)
 
print('Before :',df.dtypes)
# Create a mapping dictionary for boolean to string
bool_to_str_mapping = {True: 'True', False: 'False'}
 
# Map boolean to string using map function
df['column_name'] = df['column_name'].map(bool_to_str_mapping)
 
# Display the DataFrame
print(df)
 
print('After :',df.dtypes)


Output:

Before : column_name    bool
dtype: object
column_name
0 True
1 False
2 True
3 False
After : column_name object
dtype: object

Boolean to String in Pandas Dataframe Using replace() Method

In this example, we leverage the replace() method to substitute boolean values with their corresponding strings. The replace method is versatile and can be extended for more complex replacements.

Python3




import pandas as pd
 
# Sample DataFrame
data = {'column_name': [True, False, True, False]}
df = pd.DataFrame(data)
 
print('Before :', df.dtypes)
 
# Replace boolean values with strings using the replace method
df['column_name'] = df['column_name'].replace({True: 'True', False: 'False'})
 
# Display the DataFrame
print(df)
 
print('After :', df.dtypes)


Output:

Before : column_name    bool
dtype: object
column_name
0 True
1 False
2 True
3 False
After : column_name object
dtype: object


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads