In this tutorial, we will look at how to apply a function to each value of a pandas series.
How to apply a function on pandas series?
To apply a function on each value of a pandas series you can use the pandas series
apply() function and pass the function you want to apply as an argument. The following is the syntax:
# using pandas series apply() s_new = s.apply(your_func)
Here, s is the original pandas series you want to apply the function on, and “your_func” is the function you want to apply. You can also pass lambda functions or built-in python functions instead of a custom function.
Let’s look at some examples of applying a function to a series. First, we’ll create a sample series of names of employees in an office that we’ll be using throughout this tutorial.
import pandas as pd # pandas series of Full Names names = pd.Series(data=['Jake Peralta', 'Doug Judy', 'Charles Boyle', 'Gina Linetti']) # display the series print(names)
0 Jake Peralta 1 Doug Judy 2 Charles Boyle 3 Gina Linetti dtype: object
We now have a pandas series containing full names of employees at an office.
1. Apply a built-in function to a series
You can apply custom and built-in functions to the values of a pandas series. Let’s create a new series containing the lengths of the names in the original series.
# apply function to series name_lengths = names.apply(len) # print the returned series print(name_lengths)
0 12 1 9 2 13 3 12 dtype: int64
In the above example, we applied the python built-in
len function which gives the length of objects. Here, the returned series contains the length of names from the series “names”.
2. Apply lambda function to a series
You can also pass a lambda function as an argument to the
apply() function. Using lambda function is quite common when applying simple functions to series and dataframes without explicitly defining the function.
# apply function to series name_caps = names.apply(lambda x: x.upper()) # print the returned series print(name_caps)
0 JAKE PERALTA 1 DOUG JUDY 2 CHARLES BOYLE 3 GINA LINETTI dtype: object
In the above example, we use a lambda function to get a series with names from the original series in upper case.
3. Apply custom function to a series
You can also apply custom functions to a series using the
apply() function. Let’s create a custom function to return only the first name.
# custom function def get_first_name(n): words = n.split(" ") return words # apply function to series first_names = names.apply(get_first_name) # print the returned series print(first_names)
0 Jake 1 Doug 2 Charles 3 Gina dtype: object
The returned series contains values resulting from applying the custom function “get_first_name()” on the original series.
4. Apply function with additional arguments on series
You can also pass additional arguments when using the
apply() function. For example, let’s modify the above function to return only the first k characters of the first name where k is an additional argument.
# custom function def get_first_name(n, k): words = n.split(" ") return words[:k] # apply function to series first_names = names.apply(get_first_name, args=(4,)) # print the returned series print(first_names)
0 Jake 1 Doug 2 Char 3 Gina dtype: object
In the above example, we pass the additional argument as a tuple to the
args parameter of the
apply() function. Note that the returned series only has the first four characters of the first name.
For more on the pandas series apply() function, refer to its documentation.
With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.
Tutorials on pandas series –