When working with pandas dataframes, it might be handy to know how to quickly replace values. In this tutorial, we’ll look at how to replace values in a pandas dataframe through some examples.
How to replace values in a Pandas DataFrame?
The pandas dataframe replace()
function is used to replace values in a pandas dataframe. It allows you the flexibility to replace a single value, multiple values, or even use regular expressions for regex substitutions. The following is its syntax:
df_rep = df.replace(to_replace, value)
Here, to_replace
is the value or values to be replaced and value
is the value to replace with. By default, the pandas dataframe replace()
function returns a copy of the dataframe with the values replaced. If you want to replace the values in-place pass inplace=True
Examples
Let’s look at some of the different use-cases of the replace()
function through some examples.
1. Replace values throughout the dataframe
The replace()
function replaces all occurrences of the value with the desired value. See the example below:
import pandas as pd # sample dataframe df = pd.DataFrame({'A': ['a','b','c'], 'B':['b','c','d']}) print("Original DataFrame:\n", df) # replace b with e df_rep = df.replace('b', 'e') print("\nAfter replacing:\n", df_rep)
Output:
Original DataFrame: A B 0 a b 1 b c 2 c d After replacing: A B 0 a e 1 e c 2 c d
In the above example, the replace()
function is used to replace all the occurrences of b
in the dataframe with e
.
2. Replace values in a particular column
The pandas dataframe replace()
function allows you the flexibility to replace values in specific columns without affecting values in other columns.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
import pandas as pd # sample dataframe df = pd.DataFrame({'A': ['a','b','c'], 'B':['b','c','d']}) print("Original DataFrame:\n", df) # replace b with e df_rep = df.replace({'A': 'b'}, 'e') print("\nAfter replacing:\n", df_rep)
Output:
Original DataFrame: A B 0 a b 1 b c 2 c d After replacing: A B 0 a b 1 e c 2 c d
In the above example, we replace the occurrences of b
in just the column A
of the dataframe. For this, we pass a dictionary to the to_replace
parameter. Here the dictionary {'A': 'b'}
tells the replace function that we want to replace the value b
in the column A
. And the 'e'
passed to the value parameter use is used to replace all relevant matches.
3. Replace multiple values together
You can also have multiple replacements together. For example, if you want to replace a
with b
, b
with c
and c
with d
in the above dataframe, you can pass just a single dictionary to the replace function.
import pandas as pd # sample dataframe df = pd.DataFrame({'A': ['a','b','c'], 'B':['b','c','d']}) print("Original DataFrame:\n", df) # replace a with b, b with c, and c with d df_rep = df.replace({'a':'b', 'b':'c', 'c':'d'}) print("\nAfter replacing:\n", df_rep)
Output:
Original DataFrame: A B 0 a b 1 b c 2 c d After replacing: A B 0 b c 1 c d 2 d d
In the above example, we only pass a single dictionary to the replace()
function. The function infers the dictionary keys as values to replace in the dataframe and the corresponding keys as the values to replace them with.
4. Replace using Regex match
To replace values within a dataframe via a regular expression match, pass regex=True
to the replace function. Keep in mind that you pass the regular expression string to the to_replace
parameter and the value to replace the matches to the value parameter. Also, note that regular expressions will only substitute for strings.
import pandas as pd # sample dataframe df = pd.DataFrame({'A': ['tap','cap','map'], 'B':['cap','map', 'tap']}) print("Original DataFrame:\n", df) # replace ap with op df_rep = df.replace(to_replace='ap', value='op', regex=True) print("\nAfter replacing:\n", df_rep)
Output:
Original DataFrame: A B 0 tap cap 1 cap map 2 map tap After replacing: A B 0 top cop 1 cop mop 2 mop top
In the above example, the regular expression looks for the occurrences of ap
and replaces them with op
.
For more on the Pandas dataframe replace function, refer to its official documentation.
With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.
More on Pandas DataFrames –
- Pandas – Sort a DataFrame
- Change Order of Columns of a Pandas DataFrame
- Pandas DataFrame to a List in Python
- Pandas – Count of Unique Values in Each Column
- Pandas – Replace Values in a DataFrame
- Pandas – Filter DataFrame for multiple conditions
- Pandas – Random Sample of Rows
- Pandas – Random Sample of Columns
- Save Pandas DataFrame to a CSV file
- Pandas – Save DataFrame to an Excel file
- Create a Pandas DataFrame from Dictionary
- Convert Pandas DataFrame to a Dictionary
- Drop Duplicates from a Pandas DataFrame
- Concat DataFrames in Pandas
- Append Rows to a Pandas DataFrame
- Compare Two DataFrames for Equality in Pandas
- Get Column Names as List in Pandas DataFrame
- Select One or More Columns in Pandas
- Pandas – Rename Column Names
- Pandas – Drop one or more Columns from a Dataframe
- Pandas – Iterate over Rows of a Dataframe
- How to Reset Index of a Pandas DataFrame?
- Read CSV files using Pandas – With Examples
- Apply a Function to a Pandas DataFrame