How to replace multiple values in a Pandas DataFrame?
0

How to replace multiple values in a Pandas DataFrame?

This recipe helps you replace multiple values in a Pandas DataFrame

In [1]:
## How to replace multiple values in a Pandas DataFrame
def Snippet_104():
    print()
    print(format('How to replace multiple values in a Pandas DataFrame','*^82'))

    import warnings
    warnings.filterwarnings("ignore")

    # load libraries
    import pandas as pd
    import numpy as np

    # Create dataframe
    raw_data = {'first_name': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'],
                'last_name': ['Miller', 'Jacobson', 'Ali', 'Milner', 'Cooze'],
                'age': [42, 52, 36, 24, 73],
                'preTestScore': [-999, -999, -999, 2, 1],
                'postTestScore': [2, 2, -999, 2, -999]}

    df = pd.DataFrame(raw_data, columns = ['first_name', 'last_name', 'age', 'preTestScore', 'postTestScore'])
    print(); print(df)

    # Replace all values of -999 with NAN
    print(); print(df.replace(-999, np.nan))

    # Replace all values of -999 with 0
    print(); print(df.replace(-999, 0))

Snippet_104()
***************How to replace multiple values in a Pandas DataFrame***************

  first_name last_name  age  preTestScore  postTestScore
0      Jason    Miller   42          -999              2
1      Molly  Jacobson   52          -999              2
2       Tina       Ali   36          -999           -999
3       Jake    Milner   24             2              2
4        Amy     Cooze   73             1           -999

  first_name last_name  age  preTestScore  postTestScore
0      Jason    Miller   42           NaN            2.0
1      Molly  Jacobson   52           NaN            2.0
2       Tina       Ali   36           NaN            NaN
3       Jake    Milner   24           2.0            2.0
4        Amy     Cooze   73           1.0            NaN

  first_name last_name  age  preTestScore  postTestScore
0      Jason    Miller   42             0              2
1      Molly  Jacobson   52             0              2
2       Tina       Ali   36             0              0
3       Jake    Milner   24             2              2
4        Amy     Cooze   73             1              0

Relevant Projects

Big Data Project Build an Image Classifier for Plant Species Identification
In this machine learning project, we will use binary leaf images and extracted features, including shape, margin, and texture to accurately identify plant species using different benchmark classification techniques.
Big Data Project Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.
Big Data Project Sequence Classification with LSTM RNN in Python with Keras
In this project, we are going to work on Sequence to Sequence Prediction using IMDB Movie Review Dataset​ using Keras in Python.
Big Data Project Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.
Big Data Project Forecast Inventory demand using historical sales data in R
In this machine learning project, you will develop a machine learning model to accurately forecast inventory demand based on historical sales data.
Big Data Project PySpark Tutorial - Learn to use Apache Spark with Python
PySpark Project-Get a handle on using Python with Spark through this hands-on data processing spark python tutorial.
Big Data Project Predict Census Income using Deep Learning Models
In this project, we are going to work on Deep Learning using H2O to predict Census income.
Big Data Project Deep Learning with Keras in R to Predict Customer Churn
In this deep learning project, we will predict customer churn using Artificial Neural Networks and learn how to model an ANN in R with the keras deep learning package.
Big Data Project 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.
Big Data Project Perform Time series modelling using Facebook Prophet
In this project, we are going to talk about Time Series Forecasting to predict the electricity requirement for a particular house using Prophet.