Built with Tutify.io

    AIStudyLabs

    Pandas Series.replace() Function

    Pandas Series.replace() Function

    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 –

    <a id="h-1-quick-examples-of-series-replace"></a>

    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()

    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
    

    <a id="h-5-use-nbsp-series-replace-nbsp-function"></a>

    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
    

    <a id="h-7-series-replace-with-dict"></a>

    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

    References