Table of Contents
Converting a boolean datatype to a string in a Pandas DataFrame can be achieved by using the .astype() method. This method allows for the conversion of data types within a DataFrame, including boolean values. By specifying “str” as the parameter for the .astype() method, the boolean values will be converted to string values. This transformation can be useful for data analysis and manipulation purposes, as it allows for easier comparison and manipulation of boolean values within the DataFrame.
Convert Boolean to String in 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: objectFrom 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 .
The following tutorials explain how to perform other common tasks in pandas:
Cite this article
stats writer (2024). How can I convert a boolean datatype to a string in a Pandas DataFrame?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-convert-a-boolean-datatype-to-a-string-in-a-pandas-dataframe/
stats writer. "How can I convert a boolean datatype to a string in a Pandas DataFrame?." PSYCHOLOGICAL SCALES, 24 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-convert-a-boolean-datatype-to-a-string-in-a-pandas-dataframe/.
stats writer. "How can I convert a boolean datatype to a string in a Pandas DataFrame?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-convert-a-boolean-datatype-to-a-string-in-a-pandas-dataframe/.
stats writer (2024) 'How can I convert a boolean datatype to a string in a Pandas DataFrame?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-convert-a-boolean-datatype-to-a-string-in-a-pandas-dataframe/.
[1] stats writer, "How can I convert a boolean datatype to a string in a Pandas DataFrame?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I convert a boolean datatype to a string in a Pandas DataFrame?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
