Pandas Series.replace() – Replace Values

  • Post author:
  • Post category:Pandas / Python
  • Post last modified:January 25, 2023
Spread the love

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.

1. Quick Examples of Series Replace

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


# Below are quick examples

# 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})

2. 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)

2.1 Parameters of replace()

  • to_replace – Takes str, regex, list, dict, Series, int, float, or None
  • value – scalar, dict, list, str, regex, default None
  • inplace – bool, default False
  • limit – int, default None
  • regex –  bool or same types ato_replace, default False
  • method – {‘pad’, ‘ffill’, ‘bfill’None}

2.2 Return value of  replace()

It returns pandas series

3. 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.

4. 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.


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

5. 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) 

Yields below output.


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

6. Replace with Multiple Values

You can also replace it with the same value for multiple values. Here, it replaces all instances of 20000 and 23000 with 50000 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)

Yields below output.


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

7. Series Replace with Dict

The below examples replace from and to values by using Dict.


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

Yields below output.


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

8. Complete Example For Pandas Series Replace


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)

9. 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

Leave a Reply

You are currently viewing Pandas Series.replace() – Replace Values