Pandas Series.replace() Function

  • Post author:
  • Post category:Pandas
  • Post last modified:February 18, 2024
  • Reading time:18 mins read

Pandas Series.replace() function is used to replace values of given series with specified values. This process is done by dynamically. This method takes to_replace, value, inplace, limit, regex, and method as parameters and returns a new Series. When inplace=True it replaces an existing Series object and returns None value. In this article, I will explain Pandas replace() method syntax, and usage with examples.

Key Points –

  • Pandas Series.replace() function provides a convenient way to replace values within a Series with specified new values.
  • It offers flexibility in handling replacements by allowing various data types as inputs for both the values to be replaced and the replacement values.
  • It allows for replacing single values or a list-like subset of values within the Series.
  • Parameters include 'to_replace' which specifies the value(s) to be replaced, and ‘value’ which specifies the new value(s) to replace with.
  • The function can handle replacing values with scalar values, lists, dictionaries, or other Series objects.
  • NaN values can be replaced using this function by passing ‘NaN’ or numpy.nan as the 'to_replace' argument.

Quick Examples of Series Replace

If you are in a hurry, below are some quick examples of how to replace values in series.


# Quick examples of series replace

# Example 1: Use Series.replace() function
ser2 = ser.replace(to_replace = 20000, value = 30000)

# Example 2: Replace with multiple values
ser2 = ser.replace(to_replace=[20000,23000], value=50000)

# Example 3: Replace with same value for multiple
ser2 = ser.replace([20000,23000], 50000)

# Example 4: Replace with dict
ser2 = ser.replace({23000 : None})

Syntax of Pandas Series.replace()

Following is the syntax of Series.replace() function.


# Syntax of Series.replace()
Series.replace(to_replace=None, value=_NoDefault.no_default, *, inplace=False, limit=None, regex=False, method=_NoDefault.no_default)

Parameters of replace()

  • to_replace – This parameter specifies the value(s) to be replaced. Takes str, regex, list, dict, Series, int, float, or None
  • value – This parameter specifies the new value(s) to replace with. Scalar, dict, list, str, regex, default None
  • inplace – This parameter specifies whether to perform the operation in place or return a new Series with replaced values. Default is False.
  • limit – This parameter specifies the maximum number of replacements to make. Default is None (replace all occurrences).
  • regex –  This parameter specifies whether to interpret ‘to_replace’ as a regular expression. Default is False.
  • method – {‘pad’, ‘ffill’, ‘bfill’None} This parameter specifies the method to use when there are consecutive NaNs. Default is ‘pad’ which fills the NaNs using the previous valid observation.

Return value

The replace() function in Pandas Series returns a new Series object with the specified replacements made according to the parameters provided. If the inplace parameter is set to True, the function performs the replacement operation on the original Series itself and returns None. Otherwise, it returns a new Series object with the replacements applied.

Usage of Replace Series in Pandas

Pandas replace() function is used to replace the specified value with a given series value. The replace() function searches the entire Series and replaces every case of the specified value.

Initialize Pandas Series

Pandas Series is a one-dimensional, Index-labeled data structure available in the Pandas library. It can store all the datatypes such as strings, integers, float, and other python objects. We can access each element in the Series with the help of corresponding default indices.

Now, let’s create pandas series using a list of values.


import pandas as pd
  
# Create the Series
ser = pd.Series([20000,25000,23000,28000,55000,23000,28000])
  
# Create the Index
index = ['Java','Spark','PySpark','Pandas','NumPy','Python',"Oracle"]
  
# Set the index
ser.index = index
print(ser)

Yields below output.


# Output:
Java       20000
Spark      25000
PySpark    23000
Pandas     28000
NumPy      55000
Python     23000
Oracle     28000
dtype: int64

Use Series.replace() Function

Pandas Series.replace() function is used to find a value on a Series and replace the old values with the new ones.


# Use Series.replace() function
ser2 = ser.replace(to_replace = 20000, value = 30000)
print(ser2) 

In this example, we create a Pandas Series ser with some fee data. We then use the replace() function to replace the value 20000 with 30000. The modified Series is stored in ser2, and both the original and modified Series are printed for comparison. This example yields the below output.


# Output:
Java       30000
Spark      25000
PySpark    23000
Pandas     28000
NumPy      55000
Python     23000
Oracle     28000
dtype: int64

Replace with Multiple Values

Similarly, the replace() function in Pandas Series can also be used to replace multiple values with multiple other values.


# Replace with multiple values
ser2 = ser.replace(to_replace=[20000,23000], value=50000)
print(ser2)

# with out using param names
ser2 = ser.replace([20000,23000], 50000)
print(ser2)

In the above example, we replace both 20000 and 23000 with 50000 in the original Series ser. The resulting Series ser2 has these replacements applied. As you can see, 20000 and 23000 have been replaced by 50000, while other values remain unchanged. This example yields the below output.


# Output:
Java       50000
Spark      25000
PySpark    50000
Pandas     28000
NumPy      55000
Python     50000
Oracle     28000
dtype: int64

Series Replace with Dict

To replace specific values in a Pandas Series with None using a dictionary with the replace() function.


# Replace with dict
ser2 = ser.replace({23000 : None})
print(ser2)

In this program, {23000: None} is passed to the replace() function, indicating that we want to replace the value 23000 with None. As a result, the occurrences of 23000 in the Series are replaced with None, represented as NaN (Not a Number) in the resulting Series ser2. This example yields the below output.


# Output:
Java       20000
Spark      25000
PySpark     None
Pandas     28000
NumPy      55000
Python      None
Oracle     28000
dtype: object

Complete Example


import pandas as pd
  
# Create the Series
ser = pd.Series([20000,25000,23000,28000,55000,23000,28000])
  
# Create the Index
index = ['Java','Spark','PySpark','Pandas','NumPy','Python',"Oracle"]
  
# Set the index
ser.index = index
print(ser)

# Use Series.replace() function
ser2 = ser.replace(to_replace = 20000, value = 30000)
print(ser2) 

# Replace with multiple values
ser2 = ser.replace(to_replace=[20000,23000], value=50000)
print(ser2)

# Replace with same value for multiple
ser2 = ser.replace([20000,23000], 50000)
print(ser2)

# Replace with dict
ser2 = ser.replace({23000 : None})
print(ser2)

Frequently Asked Questions on Pandas Series Replace

What is the purpose of the replace() function in Pandas Series?

The replace() function in Pandas Series is used to replace specified values with other values within the Series, offering a convenient way to manipulate data.

Can I replace multiple values with a single value using replace()?

You can replace multiple values with a single value using the replace() function in Pandas Series. You can achieve this by passing a list of values to be replaced and specifying the single replacement value.

Does the replace() function modify the original Series?

By default, the replace() function returns a new Series with the specified replacements, leaving the original Series unchanged. However, you can modify the original Series in place by setting the inplace parameter to True.

What happens if a value to be replaced is not found in the Series?

If a value to be replaced is not found in the Series, it remains unchanged in the output after applying the replace() function. Essentially, the function ignores the values that are not present in the Series and only performs replacements for the values that match the specified criteria.

Does the replace() function handle NaN values?

The replace() function in Pandas Series can handle NaN (Not a Number) values. You can specify NaN values as part of the replacement criteria in the to_replace parameter, and you can also specify how NaN values should be treated in the replacement process.

Can I limit the number of replacements made by replace()?

You can limit the number of replacements made by the replace() function using the limit parameter. This parameter allows you to specify the maximum number of replacements to make within the Series.

Conclusion

In this article, I have explained how to replace values in pandas series using series.replace() function. It is used to replace the column value by using regex, list, dictionary, number, etc.

Happy Learning !!

Related Articles

References

Malli

Malli is an experienced technical writer with a passion for translating complex Python concepts into clear, concise, and user-friendly articles. Over the years, he has written hundreds of articles in Pandas, NumPy, Python, and takes pride in ability to bridge the gap between technical experts and end-users.

Leave a Reply