• Post author:
  • Post category:Pandas
  • Post last modified:March 27, 2024
  • Reading time:11 mins read
You are currently viewing 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.

Advertisements

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


# Below are quick examples.

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

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

2.1 Parameters of apply()

Following are the parameters of apply().

  • func – function: Python function or NumPy ufunc to apply.
  • convert_dtype – bool, default True: Try to get a better dtype for elementwise function results. If False, leave as dtype=object.
  • args – tuple: Positional arguments passed to func after the series value.
  • **kwargs – Additional keyword arguments passed to func.

2.2 Return value of apply()

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

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

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

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

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

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

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

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

11. 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 !!

References