Sling Academy
Home/Pandas/Pandas – Using Series.replace() method (3 examples)

Pandas – Using Series.replace() method (3 examples)

Last updated: February 28, 2024

Introduction

The Pandas library in Python is a powerful tool for data manipulation and analysis. Among its robust set of features, the Series.replace() method is a versatile function that allows you to replace values in a Series. This tutorial will guide you through how to use the Series.replace() method in Pandas with three practical examples, ranging from basic to advanced scenarios.

Prerequisites

To get the most out of this tutorial, you should have the following:

  • Basic understanding of Python
  • Pandas library installed

Understanding Series.replace()

The Series.replace() method in Pandas is used to replace values in a Series with another value. Its basic syntax is:

Series.replace(to_replace, value, inplace=False)

Where:

  • to_replace – The value(s) to replace.
  • value – The new value(s) for replacement.
  • inplace – If set to True, performs operation inplace and returns None.

Example 1: Basic Replacement

Let’s start with a simple example to demonstrate how to replace a single value in a Series.

import pandas as pd

# Creating a Pandas Series
s = pd.Series([1, 2, 3, 4, 5])

# Replacing '3' with '99'
s_replaced = s.replace(3, 99)

# Displaying the result
print(s_replaced)

Output:

0     1
1     2
2    99
3     4
4     5
dtype: int64

Example 2: Replacing Multiple Values

Next, let us look at how to replace multiple values in a Series at once.

import pandas as pd

# Creating a new Series
s = pd.Series(["apple", "banana", "cherry", "date"])

# Replacing 'banana' and 'cherry' with 'fruit'
s_replaced = s.replace(["banana", "cherry"], "fruit")

# Displaying the result
print(s_replaced)

Output:

0     apple
1     fruit
2     fruit
3      date
dtype: object

Example 3: Advanced Replacement Using Regular Expressions

For more complex data manipulation, Series.replace() supports using regular expressions. This is especially handy for pattern matching and replacement.

import pandas as pd

# Creating a Series with mixed patterns
s = pd.Series(["1_a", "2_b", "3_c", "4_d"])

# Replacing patterns using regex. Here, replacing anything that matches '_[a-z]' with '_X'
s_replaced = s.replace(to_replace="_[a-z]", value="_X", regex=True)

# Displaying the result
print(s_replaced)

Output:

0    1_X
1    2_X
2    3_X
3    4_X
dtype: object

Conclusion

In this tutorial, we explored the Series.replace() method in the Pandas library through three examples, illustrating its flexibility for data manipulation tasks ranging from simple value replacements to complex pattern matching with regular expressions. Mastering the Series.replace() method can significantly streamline your data preprocessing work in Python.

Next Article: Python: Turn a List of Tuples to a Pandas Series

Previous Article: Convert a Pandas Series to a Python List of Tuples

Series: Pandas Series: From Basic to Advanced

Pandas

You May Also Like

  • How to Use Pandas Profiling for Data Analysis (4 examples)
  • How to Handle Large Datasets with Pandas and Dask (4 examples)
  • Pandas – Using DataFrame.pivot() method (3 examples)
  • Pandas: How to ‘FULL JOIN’ 2 DataFrames (3 examples)
  • Pandas: Select columns whose names start/end with a specific string (4 examples)
  • 3 ways to turn off future warnings in Pandas
  • How to Integrate Pandas with Apache Spark
  • How to Use Pandas for Web Scraping and Saving Data (2 examples)
  • How to Clean and Preprocess Text Data with Pandas (3 examples)
  • Pandas json_normalize() function: Explained with examples
  • Pandas: Reading CSV and Excel files from AWS S3 (4 examples)
  • Using pandas.Series.rank() method (4 examples)
  • Pandas: Dropping columns whose names contain a specific string (4 examples)
  • Pandas: How to print a DataFrame without index (3 ways)
  • Fixing Pandas NameError: name ‘df’ is not defined
  • Pandas – Using DataFrame idxmax() and idxmin() methods (4 examples)
  • Pandas FutureWarning: ‘M’ is deprecated and will be removed in a future version, please use ‘ME’ instead
  • Pandas: Checking equality of 2 DataFrames (element-wise)
  • Understanding pandas.DataFrame.loc[] through 6 examples