This is a very common data-preprocessing requirement. For example, replacing all negative values in a column with zero or replacing all the outlier values with a sensible value, etc.
Finding and replacing specific values globally
If you need to find and replace some specific values in columns then the replace() function.
In the below example, all occurrences of the character “M” will be replaced by “Male” and all occurrences of “21” will be replaced by “30”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# Defining Employee Data import pandas as pd EmpData=pd.DataFrame({'Name': ['ram','ravi','sham','sita','gita'], 'id': [101,102,103,104,105], 'Gender': ['M','M','M','F','F'], 'Age': [21,25,24,28,25] }) # Priting data print(EmpData) # Replacing values in data globally for all the columns # Wherever you find values, replace them: M-->Male, and 21-->22 EmpDataReplaced=EmpData.replace(to_replace={'M':'Male', 21:30}, inplace=False) EmpDataReplaced |
Sample Output:

Finding and replacing specific values only for one column
Instead of replacing values for all the columns, you can restrict it to a column also.
1 2 3 4 5 6 |
# Replacing values in data for a given column only EmpData['Name']=EmpData['Name'].replace({'ram':'Ramesh', 'sham':'Suresh'}, inplace=False) # Printing the data print(EmpData) |
Sample Output:

Finding and replacing a range of values only for one column
In the below example, any age value which is either between 25 and 28 will be replaced by 40.
1 2 3 4 5 |
# Finding a range of values in a given column and replacing them # any value between 25 and 28 will be replaced by 40 FilterCondition=EmpData['Age'].between(25,28).values EmpData.loc[FilterCondition, 'Age']=40 EmpData |
Sample Output:

Using this logic, you can get data for any combination of filters and choose a column to replace its values.
Author Details
Lead Data Scientist
Farukh is an innovator in solving industry problems using Artificial intelligence. His expertise is backed with 10 years of industry experience. Being a senior data scientist he is responsible for designing the AI/ML solution to provide maximum gains for the clients. As a thought leader, his focus is on solving the key business problems of the CPG Industry. He has worked across different domains like Telecom, Insurance, and Logistics. He has worked with global tech leaders including Infosys, IBM, and Persistent systems. His passion to teach inspired him to create this website!