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 :

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 :

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:

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python | Segregate True and False value indices
- Python - Extract ith column values from jth column values
- Python | Values till False element
- Python - False values Frequency
- Python | Get indices of True values in a binary list
- Python program to fetch the indices of true values in a Boolean list
- Replace all the NaN values with Zero's in a column of a Pandas dataframe
- Python | Test for False list
- Python | Prefix Sum Subarray till False value
- Python | Remove False row from matrix
- editable=False - Django Built-in Field Validation
- Python - False indices in a boolean list
- Python | Pandas Series.str.replace() to replace text in a series
- replace() in Python to replace a substring
- Python | First occurrence of True number
- Python | Count true booleans in a list
- Python | Test if any list element returns true for condition
- blank=True - Django Built-in Field Validation
- null=True - Django Built-in Field Validation
- unique=True - Django Built-in Field Validation
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

