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 –
- The
apply()
function allows for applying a function along the values of a Series, enabling flexible data manipulation. - It can accept user-defined functions, which can be useful for customized operations not covered by built-in methods.
- The function passed to
apply()
can take additional arguments, allowing for more complex operations. - It can be used to perform element-wise operations, transforming each value in the Series based on the specified function.
- You can use
lambda
functions withapply()
for concise and inline function definitions. - Using
apply()
can be less efficient than vectorized operations, so it is essential to consider performance impacts on large datasets.
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 )
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)
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.
Return value of apply()
It returns a Series object the result will be a DataFrame.
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
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
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.
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
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
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
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)
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.
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 !!
Related Articles
- Pandas Iterate Over Series
- Pandas Series filter() Function
- Create Pandas Series in Python
- How to Reshape Pandas Series
- How to Rename a Pandas Series
- Pandas Series.min() Function
- Pandas Convert Series to Json
- Pandas Get Floor or Ceil of Series
- How to Convert List to Pandas Series
- How to Get Index of Series in Pandas
- Pandas Series reset_index() Function
- How to Generate Time Series Plot in Pandas