@@ -2064,13 +2064,84 @@ def apply(self, func, convert_dtype=True, args=(), **kwds):
2064
2064
Positional arguments to pass to function in addition to the value
2065
2065
Additional keyword arguments will be passed as keywords to the function
2066
2066
2067
+ Returns
2068
+ -------
2069
+ y : Series or DataFrame if func returns a Series
2070
+
2067
2071
See also
2068
2072
--------
2069
2073
Series.map: For element-wise operations
2070
2074
2071
- Returns
2072
- -------
2073
- y : Series or DataFrame if func returns a Series
2075
+ Examples
2076
+ --------
2077
+
2078
+ Create a series with typical summer temperatures for each city.
2079
+
2080
+ >>> import pandas as pd
2081
+ >>> import numpy as np
2082
+ >>> series = pd.Series([20, 21, 12], index=['London',
2083
+ ... 'New York','Helsinki'])
2084
+ London 20
2085
+ New York 21
2086
+ Helsinki 12
2087
+ dtype: int64
2088
+
2089
+ Square the values by defining a function and passing it as an
2090
+ argument to ``apply()``.
2091
+
2092
+ >>> def square(x):
2093
+ ... return x**2
2094
+ >>> series.apply(square)
2095
+ London 400
2096
+ New York 441
2097
+ Helsinki 144
2098
+ dtype: int64
2099
+
2100
+ Square the values by passing an anonymous function as an
2101
+ argument to ``apply()``.
2102
+
2103
+ >>> series.apply(lambda x: x**2)
2104
+ London 400
2105
+ New York 441
2106
+ Helsinki 144
2107
+ dtype: int64
2108
+
2109
+ Define a custom function that needs additional positional
2110
+ arguments and pass these additional arguments using the
2111
+ ``args`` keyword.
2112
+
2113
+ >>> def subtract_custom_value(x, custom_value):
2114
+ ... return x-custom_value
2115
+
2116
+ >>> series.apply(subtract_custom_value, args=(5,))
2117
+ London 15
2118
+ New York 16
2119
+ Helsinki 7
2120
+ dtype: int64
2121
+
2122
+ Define a custom function that takes keyword arguments
2123
+ and pass these arguments to ``apply``.
2124
+
2125
+ >>> def add_custom_values(x, **kwargs):
2126
+ ... for month in kwargs:
2127
+ ... x+=kwargs[month]
2128
+ ... return x
2129
+
2130
+ >>> series.apply(add_custom_values, june=30, july=20, august=25)
2131
+ London 95
2132
+ New York 96
2133
+ Helsinki 87
2134
+ dtype: int64
2135
+
2136
+ Use a function from the Numpy library.
2137
+
2138
+ >>> series.apply(np.log)
2139
+ London 2.995732
2140
+ New York 3.044522
2141
+ Helsinki 2.484907
2142
+ dtype: float64
2143
+
2144
+
2074
2145
"""
2075
2146
if len (self ) == 0 :
2076
2147
return self ._constructor (dtype = self .dtype ,
0 commit comments