![]() |
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 DataframeBelow are some examples by which we can convert Boolean to String in Pandas DataFrame in Python: Boolean to String in Pandas Dataframe Using
|
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
apply
Function with LambdaIn 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.
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
map()
FunctionIn 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.
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
replace()
MethodIn 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.
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
Reffered: https://www.geeksforgeeks.org
Geeks Premier League |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 16 |