Built with Tutify.io

    AIStudyLabs

    Pandas Series apply() Function Usage

    Pandas Series apply() Function Usage

    Pandas Series.apply() function is used to execute a function for each element in a Series. The function allows three parameters func, convert_dtype, and args. In this article, I will explain how to use pandas apply() function with arguments to a series by using Series.apply() function. func param is used with Lambda expression. Lambda expressions are utilized to construct anonymous functions.

    Key Points –

    <a id="h-quick-examples-of-apply-function-to-series"></a>

    Quick Examples of apply() Function to Series

    If you are in hurry below are some quick examples of the apply function with arguments to a series.

    
    # Quick Examples of apply() Function to Series
    
    # Example 1: Pass square() function 
    # As an argument of apply() & get series
    def square(x):
        return x ** 3
    ser2 = ser.apply(square)
    
    # Example 2: Pass lambda expression 
    # As an argument apply() function
    ser2 = ser.apply(lambda x: x ** 3)
    
    # Example 3: Pass subtract_custom_value() function 
    # As an argument of apply() & get series
    def subtract_custom_value(x, custom_value):
        return x - custom_value
    ser2 = ser.apply(subtract_custom_value, args=(2,))
    
    # Example 4: Pass add_custom_values() function 
    # As an argument of apply() & get series 
    def add_custom_values(x, **kwargs):
        for month in kwargs:
            x += kwargs[month]
        return x
    ser2 = ser.apply(add_custom_values, may=15, june=20, july=10)
    
    # Example 5: Apply function numpy.log to series
    def add_custom_values(x, **kwargs):
        for month in kwargs:
            x += kwargs[month]
        return x
    ser2 = ser.apply(np.log)
    
    # Example 6: Use series.apply() function & lambda expression
    ser = pd.Series(['Java','Spark','PySpark','NumPy','Python',"Oracle"])
    ser2 = ser.apply(lambda x : 'Hyperion' if x =='Java' else x )
    

    <a id="h-syntax-of-pandas-series-apply"></a>

    Syntax of Pandas Series.apply()

    Following is the syntax to use Series.apply() function.

    
    # Syntax of Series.apply()
    Series.apply(func, convert_dtype=True, args=(), **kwargs)
    

    <a id="h-parameters-of-apply"></a>

    Parameters of apply()

    Following are the parameters of apply().

    <a id="h-return-value-of-apply"></a>

    Return value of apply()

    It returns a Series object the result will be a DataFrame.

    <a id="h-create-pandas-series"></a>

    Create Pandas Series

    Pandas Series is a one-dimensional, Index-labeled data structure available only 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 list of values.

    
    import pandas as pd
    import numpy as np
      
    # Create the Series
    ser = pd.Series([20,25,15,10,5,20,30], ['Java','Spark','PySpark','Pandas','NumPy','Python',"Oracle"])
    print(ser)
    

    Yields below output.

    
    # Output:
    Java       20
    Spark      25
    PySpark    15
    Pandas     10
    NumPy       5
    Python     20
    Oracle     30
    dtype: int64
    

    <a id="h-call-a-function-on-pandas-series-using-apply"></a>

    Call a Function on Pandas Series using apply()

    Let's compute the square values of pandas Series elements by using the square() custom function and calling it using apply() function, it returns the square values.

    
    # Pass square() function as an argument of apply() & get series
    def square(x):
        return x ** 3
    ser2 = ser.apply(square)
    print(ser2)
    

    Yields below output.

    
    # Output:
    Java        8000
    Spark      15625
    PySpark     3375
    Pandas      1000
    NumPy        125
    Python      8000
    Oracle     27000
    dtype: int64
    

    <a id="h-use-lambda-expression-with-apply-function"></a>

    Use Lambda Expression with apply() Function

    Pass lambda expression as an argument to apply() function. Series.apply() it will return pandas Series. Lambda expressions are utilized to construct anonymous functions.

    
    # Pass lambda expression as an argument apply() function
    ser2 = ser.apply(lambda x: x ** 3)
    print(ser2)
    

    Yields the same output as above.

    <a id="h-function-with-arguments-to-apply"></a>

    Function with arguments to apply()

    Now let's see how to pass arguments to a custom function while using it with apply() function on Pandas Series object.

    
    # Pass subtract_custom_value() function 
    # As an argument of apply() & get series
    def subtract_custom_value(x, custom_value):
        return x - custom_value
    ser2 = ser.apply(subtract_custom_value, args=(2,))
    print(ser2)
    

    Yields below output.

    
    # Output:
    Java       18
    Spark      23
    PySpark    13
    Pandas      8
    NumPy       3
    Python     18
    Oracle     28
    dtype: int64
    

    <a id="h-using-kwargs-with-apply"></a>

    Using kwargs with apply()

    Let's define a custom function that takes keyword arguments and pass it as an argument to apply() function.

    
    # Pass add_custom_values() function 
    # As an argument of apply() & get series 
    def add_custom_values(x, **kwargs):
        for month in kwargs:
            x += kwargs[month]
        return x
    ser2 = ser.apply(add_custom_values, may=15, june=20, july=10)
    print(ser2)
    

    Yields below output.

    
    # Output:
    Java       65
    Spark      70
    PySpark    60
    Pandas     55
    NumPy      50
    Python     65
    Oracle     75
    dtype: int64
    

    <a id="h-use-series-apply-function-amp-lambda-expression"></a>

    Use Series.apply() Function & Lambda Expression

    We can apply a lambda expression using Series.apply() function, the below example changes the course name to ‘Hyperion’ if the course is ‘Java’.

    
    # Use series.apply() function & lambda expression
    ser = pd.Series(['Java','Spark','PySpark','NumPy','Python',"Oracle"])
    ser2 = ser.apply(lambda x : 'Hyperion' if x =='Java' else x )
    print(ser2)
    

    Yields below output.

    
    # Output:
    0    Hyperion
    1       Spark
    2     PySpark
    3       NumPy
    4      Python
    5      Oracle
    dtype: object
    

    <a id="h-complete-example"></a>

    Complete Example

    
    import pandas as pd
    import numpy as np
      
    # Create the Series
    ser = pd.Series([20,25,15,10,5,20,30], ['Java','Spark','PySpark','Pandas','NumPy','Python',"Oracle"])
    print(ser)
    
    # Pass square() function as an argument of apply() & get series
    def square(x):
        return x ** 3
    ser2 = ser.apply(square)
    print(ser2)
    
    # Pass lambda expression as an argument apply() function
    ser2 = ser.apply(lambda x: x ** 3)
    print(ser2)
    
    # Pass subtract_custom_value() function 
    # As an argument of apply() & get series
    def subtract_custom_value(x, custom_value):
        return x - custom_value
    ser2 = ser.apply(subtract_custom_value, args=(2,))
    print(ser2)
    
    # Pass add_custom_values() function 
    # As an argument of apply() & get series 
    def add_custom_values(x, **kwargs):
        for month in kwargs:
            x += kwargs[month]
        return x
    ser2 = ser.apply(add_custom_values, may=15, june=20, july=10)
    print(ser2)
    
    # Apply function numpy.log to series
    def add_custom_values(x, **kwargs):
        for month in kwargs:
            x += kwargs[month]
        return x
    ser2 = ser.apply(np.log)
    print(ser2)
    
    # Use series.apply() function & lambda expression
    ser = pd.Series(['Java','Spark','PySpark','NumPy','Python',"Oracle"])
    ser2 = ser.apply(lambda x : 'Hyperion' if x =='Java' else x )
    print(ser2)
    

    <a id="h-faq-on-pandas-series-apply-function"></a>

    FAQ on Pandas Series apply() Function

    The apply() function is used to apply a custom function or a predefined operation to each element in a Pandas Series. It allows for element-wise transformations and computations.

    You can pass additional arguments to the function using the args parameter. These arguments are applied to each element in the Series.

    apply() is slower compared to vectorized operations because it processes each element individually. Vectorized methods are optimized at the C level for performance.

    apply() skips NaN values by default. The function is not applied to missing values, and they remain NaN in the result.

    map() is generally faster than apply() for Series transformations as it is optimized for element-wise operations, but apply() is more flexible for complex computations.

    <a id="h-conclusion"></a>

    Conclusion

    In this article, I have explained how to use Python Pandas Series.apply() function to invoke the passed function on each element of the given series object with examples.

    Happy Learning !!

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-references"></a>

    References