Finding and replacing values in a pandas data frame

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”.

Sample Output:

Finding and replacing specific values in a pandas dataframe
Finding and replacing specific values in a pandas dataframe


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.

Sample Output:

Finding and replacing data in one single column


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.

Sample Output:

Finding and replacing a range of values in pandas dataframe
Finding and replacing a range of values in pandas dataframe

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!

Leave a Reply!

Your email address will not be published. Required fields are marked *