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 Nonevalue
– This parameter specifies the new value(s) to replace with. Scalar, dict, list, str, regex, default Noneinplace
– 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
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.
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.
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
.
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.
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.
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
- How to Convert List to Pandas Series
- Pandas Remove Elements From Series
- Add Column Name to Pandas Series
- Create a Set From a Series in Pandas
- Check Values of Pandas Series is Unique
- Pretty Print Pandas DataFrame or Series
- Pandas Get First Column of DataFrame as Series
- Convert GroupBy output from Series to DataFrame
- Apply Multiple Filters to Pandas DataFrame or Series
- How to append Pandas Series?
- Check values are Pandas Series unique
- How to rename Pandas Series?
- Pandas Series astype() Function
- Pandas Series where() Function
- How to get floor or ceil values of Pandas Series?
- How to reshape the Pandas Series?
- How to get Values from Pandas Series?
- Pandas Series groupby() Function with Examples