Pandas Series: apply() function
Invoke a python function on values of Pandas series
The apply() function is used to invoke a python function on values of Series.
Syntax:
Series.apply(self, func, convert_dtype=True, args=(), **kwds)
Parameters:
Name | Description | Type/Default Value | Required / Optional |
---|---|---|---|
func | Python function or NumPy ufunc to apply. | function | Required |
convert_dtype | Try to find better dtype for elementwise function results. If False, leave as dtype=object. | bool Default Value: True |
Required |
args | Positional arguments passed to func after the series value. | tuple | Required |
**kwds | Additional keyword arguments passed to func. | Required |
Returns: Series or DataFrame
If func returns a Series object the result will be a DataFrame.
Example - Create a series with typical summer temperatures for each city:
Python-Pandas Code:
import numpy as np
import pandas as pd
s = pd.Series([31, 27, 11],
index=['Beijing', 'Los Angeles', 'Berlin'])
s
Output:
Beijing 31 Los Angeles 27 Berlin 11 dtype: int64
Example - Square the values by defining a function and passing it as an argument to apply():
Python-Pandas Code:
import numpy as np
import pandas as pd
s = pd.Series([31, 27, 11],
index=['Beijing', 'Los Angeles', 'Berlin'])
def square(x):
return x ** 2
s.apply(square)
Output:
Beijing 961 Los Angeles 729 Berlin 121 dtype: int64
Example - Square the values by passing an anonymous function as an argument to apply():
Python-Pandas Code:
import numpy as np
import pandas as pd
s = pd.Series([31, 27, 11],
index=['Beijing', 'Los Angeles', 'Berlin'])
def square(x):
return x ** 2
s.apply(lambda x: x ** 2)
Output:
Beijing 961 Los Angeles 729 Berlin 121 dtype: int64
Example - Define a custom function that needs additional positional arguments and pass these additional arguments using the args keyword:
Python-Pandas Code:
import numpy as np
import pandas as pd
s = pd.Series([31, 27, 11],
index=['Beijing', 'Los Angeles', 'Berlin'])
def subtract_custom_value(x, custom_value):
return x - custom_value
s.apply(subtract_custom_value, args=(4,))
Output:
Beijing 27 Los Angeles 23 Berlin 7 dtype: int64
Example - Define a custom function that takes keyword arguments and pass these arguments to apply:
Python-Pandas Code:
import numpy as np
import pandas as pd
s = pd.Series([31, 27, 11],
index=['Beijing', 'Los Angeles', 'Berlin'])
def add_custom_values(x, **kwargs):
for month in kwargs:
x += kwargs[month]
return x
s.apply(add_custom_values, november=18, december=16, january=15)
Output:
Beijing 80 Los Angeles 76 Berlin 60 dtype: int64
Example - Use a function from the Numpy library:
Python-Pandas Code:
import numpy as np
import pandas as pd
s = pd.Series([31, 27, 11],
index=['Beijing', 'Los Angeles', 'Berlin'])
def add_custom_values(x, **kwargs):
for month in kwargs:
x += kwargs[month]
return x
s.apply(np.log)
Output:
Beijing 3.433987 Los Angeles 3.295837 Berlin 2.397895 dtype: float64
Previous: Compute the dot product between the Series and the columns in Pandas
Next: Aggregation in Pandas
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/pandas/series/series-apply.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics