@@ -3288,23 +3288,27 @@ def transform(self, func, axis=0, *args, **kwargs):
3288
3288
3289
3289
def apply (self , func , convert_dtype = True , args = (), ** kwds ):
3290
3290
"""
3291
- Invoke function on values of Series. Can be ufunc (a NumPy function
3292
- that applies to the entire Series) or a Python function that only works
3293
- on single values
3291
+ Invoke function on values of Series.
3292
+
3293
+ Can be ufunc (a NumPy function that applies to the entire Series) or a
3294
+ Python function that only works on single values.
3294
3295
3295
3296
Parameters
3296
3297
----------
3297
3298
func : function
3298
- convert_dtype : boolean, default True
3299
+ Python function or NumPy ufunc to apply.
3300
+ convert_dtype : bool, default True
3299
3301
Try to find better dtype for elementwise function results. If
3300
- False, leave as dtype=object
3302
+ False, leave as dtype=object.
3301
3303
args : tuple
3302
- Positional arguments to pass to function in addition to the value
3303
- Additional keyword arguments will be passed as keywords to the function
3304
+ Positional arguments passed to func after the series value.
3305
+ **kwds
3306
+ Additional keyword arguments passed to func.
3304
3307
3305
3308
Returns
3306
3309
-------
3307
- y : Series or DataFrame if func returns a Series
3310
+ Series or DataFrame
3311
+ If func returns a Series object the result will be a DataFrame.
3308
3312
3309
3313
See Also
3310
3314
--------
@@ -3314,12 +3318,11 @@ def apply(self, func, convert_dtype=True, args=(), **kwds):
3314
3318
3315
3319
Examples
3316
3320
--------
3317
-
3318
3321
Create a series with typical summer temperatures for each city.
3319
3322
3320
- >>> series = pd.Series([20, 21, 12], index=['London' ,
3321
- ... ' New York','Helsinki'])
3322
- >>> series
3323
+ >>> s = pd.Series([20, 21, 12],
3324
+ ... index=['London', ' New York', 'Helsinki'])
3325
+ >>> s
3323
3326
London 20
3324
3327
New York 21
3325
3328
Helsinki 12
@@ -3329,8 +3332,8 @@ def apply(self, func, convert_dtype=True, args=(), **kwds):
3329
3332
argument to ``apply()``.
3330
3333
3331
3334
>>> def square(x):
3332
- ... return x** 2
3333
- >>> series .apply(square)
3335
+ ... return x ** 2
3336
+ >>> s .apply(square)
3334
3337
London 400
3335
3338
New York 441
3336
3339
Helsinki 144
@@ -3339,7 +3342,7 @@ def apply(self, func, convert_dtype=True, args=(), **kwds):
3339
3342
Square the values by passing an anonymous function as an
3340
3343
argument to ``apply()``.
3341
3344
3342
- >>> series .apply(lambda x: x** 2)
3345
+ >>> s .apply(lambda x: x ** 2)
3343
3346
London 400
3344
3347
New York 441
3345
3348
Helsinki 144
@@ -3350,9 +3353,9 @@ def apply(self, func, convert_dtype=True, args=(), **kwds):
3350
3353
``args`` keyword.
3351
3354
3352
3355
>>> def subtract_custom_value(x, custom_value):
3353
- ... return x- custom_value
3356
+ ... return x - custom_value
3354
3357
3355
- >>> series .apply(subtract_custom_value, args=(5,))
3358
+ >>> s .apply(subtract_custom_value, args=(5,))
3356
3359
London 15
3357
3360
New York 16
3358
3361
Helsinki 7
@@ -3363,18 +3366,18 @@ def apply(self, func, convert_dtype=True, args=(), **kwds):
3363
3366
3364
3367
>>> def add_custom_values(x, **kwargs):
3365
3368
... for month in kwargs:
3366
- ... x+= kwargs[month]
3369
+ ... x += kwargs[month]
3367
3370
... return x
3368
3371
3369
- >>> series .apply(add_custom_values, june=30, july=20, august=25)
3372
+ >>> s .apply(add_custom_values, june=30, july=20, august=25)
3370
3373
London 95
3371
3374
New York 96
3372
3375
Helsinki 87
3373
3376
dtype: int64
3374
3377
3375
3378
Use a function from the Numpy library.
3376
3379
3377
- >>> series .apply(np.log)
3380
+ >>> s .apply(np.log)
3378
3381
London 2.995732
3379
3382
New York 3.044522
3380
3383
Helsinki 2.484907
0 commit comments