apply(self, func: 'AggFuncType', convert_dtype: 'bool' = True, args: 'tuple[Any, ...]' = (), **kwargs) -> 'DataFrame | Series'
Can be ufunc (a NumPy function that applies to the entire Series) or a Python function that only works on single values.
Functions that mutate the passed object can produce unexpected behavior or errors and are not supported. See gotchas.udf-mutation
for more details.
Python function or NumPy ufunc to apply.
Try to find better dtype for elementwise function results. If False, leave as dtype=object. Note that the dtype is always preserved for some extension array dtypes, such as Categorical.
Positional arguments passed to func after the series value.
Additional keyword arguments passed to func.
If func returns a Series object the result will be a DataFrame.
Invoke function on values of Series.
Series.agg
Only perform aggregating type operations.
Series.map
For element-wise operations.
Series.transform
Only perform transforming type operations.
Create a series with typical summer temperatures for each city.
This example is valid syntax, but we were not able to check execution>>> s = pd.Series([20, 21, 12],
... index=['London', 'New York', 'Helsinki'])
... s London 20 New York 21 Helsinki 12 dtype: int64
Square the values by defining a function and passing it as an argument to apply()
.
>>> def square(x):
... return x ** 2
... s.apply(square) London 400 New York 441 Helsinki 144 dtype: int64
Square the values by passing an anonymous function as an argument to apply()
.
>>> s.apply(lambda x: x ** 2) London 400 New York 441 Helsinki 144 dtype: int64
Define a custom function that needs additional positional arguments and pass these additional arguments using the args
keyword.
>>> def subtract_custom_value(x, custom_value):This example is valid syntax, but we were not able to check execution
... return x - custom_value
>>> s.apply(subtract_custom_value, args=(5,)) London 15 New York 16 Helsinki 7 dtype: int64
Define a custom function that takes keyword arguments and pass these arguments to apply
.
>>> def add_custom_values(x, **kwargs):This example is valid syntax, but we were not able to check execution
... for month in kwargs:
... x += kwargs[month]
... return x
>>> s.apply(add_custom_values, june=30, july=20, august=25) London 95 New York 96 Helsinki 87 dtype: int64
Use a function from the Numpy library.
This example is valid syntax, but we were not able to check execution>>> s.apply(np.log) London 2.995732 New York 3.044522 Helsinki 2.484907 dtype: float64See :
The following pages refer to to this document either explicitly or contain code examples using this.
pandas.core.groupby.generic.SeriesGroupBy.apply
pandas.core.series.Series.aggregate
pandas.core.arrays.categorical.Categorical.map
pandas.core.groupby.groupby.GroupBy.apply
pandas.core.series.Series.transform
pandas.core.indexes.category.CategoricalIndex.map
pandas.core.series.Series.map
Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.
Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)
SVG is more flexible but power hungry; and does not scale well to 50 + nodes.
All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them