How can I convert Boolean to String in Pandas DataFrame?

Pandas DataFrame provides a built-in function astype() to convert Boolean to String. This function takes the DataFrame with Boolean values and returns the same DataFrame with the values converted to string by using the argument dtype = ‘str’. This astype() function provides a convenient way to convert the Boolean values to strings in the Pandas DataFrame.


You can use the following basic syntax to convert a Boolean column to a string column in a pandas DataFrame:

df['my_bool_column'] = df['my_bool_column'].replace({True: 'True', False: 'False'})

This particular example replaces each True value with the string ‘True’ and each False value with the string ‘False’ in the column called my_bool_column.

The following example shows how to use this syntax in practice.

Example: Convert Boolean to String in Pandas

Suppose we have the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'B', 'C', 'D', 'E', 'F', 'G'],
                   'points': [18,20, 25, 40, 34, 32, 19],
                   'all_star': [True, False, True, True, True, False, False],
                   'starter': [False, True, True, True, False, False, False]})

#view DataFrame
print(df)

  team  points  all_star  starter
0    A      18      True    False
1    B      20     False     True
2    C      25      True     True
3    D      40      True     True
4    E      34      True    False
5    F      32     False    False
6    G      19     False    False

We can use the dtypes function to check the data type of each column in the DataFrame:

#view data type of each column
print(df.dtypes)

team        object
points       int64
all_star      bool
starter       bool
dtype: object

From the output we can see that the all_star and starter columns are both Booleans.

We can use the following syntax to convert the all_star column to a string column:

#convert Boolean values in all_star column to strings
df['all_star'] = df['all_star'].replace({True: 'True', False: 'False'})

#view updated DataFrame
print(df)

  team  points all_star  starter
0    A      18     True    False
1    B      20    False     True
2    C      25     True     True
3    D      40     True     True
4    E      34     True    False
5    F      32    False    False
6    G      19    False    False

#view updated data types of each column
print(df.dtypes)

team        object
points       int64
all_star    object
starter       bool
dtype: object

From the output we can see that the all_star column has been converted to a string column.

To convert the all_star and starter columns both from Boolean to strings, we can use the following syntax:

#convert Boolean values in all_star and starter columns to strings
df[['all_star', 'starter']] = df[['all_star', 'starter']].replace({True: 'True', False: 'False'})

#view updated DataFrame
print(df)

  team  points all_star  starter
0    A      18     True    False
1    B      20    False     True
2    C      25     True     True
3    D      40     True     True
4    E      34     True    False
5    F      32    False    False
6    G      19    False    False

#view updated data types of each column
print(df.dtypes)

team        object
points       int64
all_star    object
starter     object
dtype: object

From the output we can see that both Boolean columns have been converted to strings.

Note: You can find the complete documentation for the pandas replace() function .

x