Thursday, June 11, 2026
HomeLanguagesReplace the column contains the values ‘yes’ and ‘no’ with True and...

Replace the column contains the values ‘yes’ and ‘no’ with True and False In Python-Pandas

Let’s discuss a program To change the values from a column that contains the values ‘YES’ and ‘NO’ with TRUE and FALSE
 

First, Let’s see a dataset.

Code:

Python3




# import pandas library
import pandas as pd
   
# load csv file
df = pd.read_csv("supermarkets.csv")
   
# show the dataframe
df


Output : 

Dataframe with yes and no

For downloading the used csv file Click Here.

Now, Let’s see the multiple ways to do this task:

Method 1: Using Series.map()
This method is used to map values from two series having one column the same. 

Syntax: Series.map(arg, na_action=None). 
Return type: Pandas Series with the same as an index as a caller. 

Example: Replace the ‘commissioned’ column contains the values ‘yes’ and ‘no’ with True and False.
Code:

Python3




# import pandas library
import pandas as pd
   
# load csv file
df = pd.read_csv("supermarkets.csv")
   
# replace the ‘commissioned' column contains
# the values 'yes' and 'no'  with 
# True and  False:
df['commissioned'] = df['commissioned'].map(
                   {'yes':True ,'no':False})
  
# show the dataframe
df


Output : 

Dataframe with true and false

Method 2: Using DataFrame.replace()
This method is used to replace a string, regex, list, dictionary, series, number, etc. from a data frame. 

Syntax: DataFrame.replace(to_replace=None, value=None, inplace=False, limit=None, regex=False, method=’pad’, axis=None) 
Return type: Updated Data frame 

Example: Replace the ‘commissioned’ column contains the values ‘yes’ and ‘no’ with True and False.
Code:

Python3




# import pandas library
import pandas as pd
  
# load csv file
df = pd.read_csv("supermarkets.csv")
  
# replace the ‘commissioned' column 
# contains the values 'yes' and 'no'
#  with True and  False:
df = df.replace({'commissioned': {'yes': True
                                'no': False}})
  
# show the dataframe
df


Output: 

dataframe with true false

 

RELATED ARTICLES

2 COMMENTS

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6897 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS