How to replace multiple values in a Pandas DataFrame?
DATA MUNGING DATA CLEANING PYTHON MACHINE LEARNING RECIPES PANDAS CHEATSHEET     ALL TAGS

How to replace multiple values in a Pandas DataFrame?

How to replace multiple values in a Pandas DataFrame?

This recipe helps you replace multiple values in a Pandas DataFrame

0

Recipe Objective

Have you ever tried to change multiple values in a dataframe at once? We can do this very easily by replacing the values with another using a simple python code.

So this recipe is a short example on how to replace multiple values in a dataframe. Let's get started.

Step 1 - Import the library

import pandas as pd import numpy as np

Here we have imported Pandas and Numpy which are very general libraries.

Step 2 - Setup the Data

Let us create a simple dataset and convert it to a dataframe. This is a dataset of city with different features in it like City_level, City_pool, Rating, City_port and City_Temperature. We have converted this dataset into a dataframe with its features as columns.

city_data = {'city_level': [1, 3, 1, 2, 2, 3, 1, 1, 2, 3], 'city_pool' : ['y','y','n','y','n','n','y','n','n','y'], 'Rating': [1, 5, 3, 4, 1, 2, 3, 5, 3, 4], 'City_port': [0, 1, 0, 1, 0, 0, 1, 1, 0, 1], 'city_temperature': ['low', 'medium', 'medium', 'high', 'low','low', 'medium', 'medium', 'high', 'low']} df = pd.DataFrame(city_data, columns = ['city_level', 'city_pool', 'Rating', 'City_port', 'city_temperature'])

Step 3 - Replacing the values and Printing the dataset

So let us consider that first we want to print the initial dataset and then we want to replace digit 1 (where ever it is present in the dataset) with the string 'one'. Finally we want to view the new dataset with the changes.

So for this we have to use replace function which have 3 important parameters in it.

  • to_replace : In this we have to pass the data of any type(string, int, floatetc) which we want to replace.
  • value : In this we have to pass the data of any type(string, int, floatetc) which we want to insert in the place of the data we want to replace.
  • inplace : It is a boolean parameter with default as False. If true it will keep the changes that is done by the function.
print(df) df = df.replace(1, 'One') print(); print(df)

Step 5 - Observing the changes in the dataset

Once we run the above code snippet, we will see that the all the 1s in the dataset will be changed to 'one'.

   city_level city_pool  Rating  City_port city_temperature
0           1         y       1          0              low
1           3         y       5          1           medium
2           1         n       3          0           medium
3           2         y       4          1             high
4           2         n       1          0              low
5           3         n       2          0              low
6           1         y       3          1           medium
7           1         n       5          1           medium
8           2         n       3          0             high
9           3         y       4          1              low

  city_level city_pool Rating City_port city_temperature
0        One         y    One         0              low
1          3         y      5       One           medium
2        One         n      3         0           medium
3          2         y      4       One             high
4          2         n    One         0              low
5          3         n      2         0              low
6        One         y      3       One           medium
7        One         n      5       One           medium
8          2         n      3         0             high
9          3         y      4       One              low

Relevant Projects

Data Science Project in Python on BigMart Sales Prediction
The goal of this data science project is to build a predictive model and find out the sales of each product at a given Big Mart store.

Resume parsing with Machine learning - NLP with Python OCR and Spacy
In this machine learning resume parser example we use the popular Spacy NLP python library for OCR and text classification.

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

Predict Macro Economic Trends using Kaggle Financial Dataset
In this machine learning project, you will uncover the predictive value in an uncertain world by using various artificial intelligence, machine learning, advanced regression and feature transformation techniques.

Music Recommendation System Project using Python and R
Machine Learning Project - Work with KKBOX's Music Recommendation System dataset to build the best music recommendation engine.

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

German Credit Dataset Analysis to Classify Loan Applications
In this data science project, you will work with German credit dataset using classification techniques like Decision Tree, Neural Networks etc to classify loan applications using R.

Predict Credit Default | Give Me Some Credit Kaggle
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

Ensemble Machine Learning Project - All State Insurance Claims Severity Prediction
In this ensemble machine learning project, we will predict what kind of claims an insurance company will get. This is implemented in python using ensemble machine learning algorithms.

Predict Employee Computer Access Needs in Python
Data Science Project in Python- Given his or her job role, predict employee access needs using amazon employee database.