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

Updated: February 28, 2024 By: Guest Contributor Post a comment

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.