Skip to content

Commit 52db356

Browse files
author
mdeboc
committed
DOC: update the pandas.DataFrame.apply docstring
1 parent 7c14e4f commit 52db356

File tree

1 file changed

+52
-35
lines changed

1 file changed

+52
-35
lines changed

pandas/core/frame.py

Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4818,66 +4818,82 @@ def aggregate(self, func, axis=0, *args, **kwargs):
48184818

48194819
def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None,
48204820
result_type=None, args=(), **kwds):
4821-
"""Applies function along an axis of the DataFrame.
4821+
"""
4822+
Apply a function along an axis of the `Series`.
48224823
4823-
Objects passed to functions are Series objects having index
4824-
either the DataFrame's index (axis=0) or the columns (axis=1).
4825-
Final return type depends on the return type of the applied function,
4826-
or on the `result_type` argument.
4824+
Objects passed to the function are `Series` objects having as index
4825+
either the DataFrame's index (`axis=0`)
4826+
or the DataFrame's columns (`axis=1`).
4827+
If `result_type` is None, the final return type is the return
4828+
type of the applied function.
4829+
Otherwise, it depends on the `result_type` argument.
48274830
48284831
Parameters
48294832
----------
48304833
func : function
4831-
Function to apply to each column/row
4834+
Function to apply to each column or row.
48324835
axis : {0 or 'index', 1 or 'columns'}, default 0
4833-
* 0 or 'index': apply function to each column
4834-
* 1 or 'columns': apply function to each row
4836+
Axis along which the function is applied:
4837+
4838+
* 0 or 'index': apply function to each column.
4839+
* 1 or 'columns': apply function to each row.
48354840
broadcast : boolean, optional
4836-
For aggregation functions, return object of same size with values
4837-
propagated
4841+
Only relevant for aggregation functions:
4842+
4843+
* `False` or `None` : returns a `Series` whose length is the length
4844+
of the index or the number of columns (based on the `axis`
4845+
parameter)
4846+
* `True` : results will be broadcast to the original shape
4847+
of the frame, the original index and columns will be retained.
48384848
48394849
.. deprecated:: 0.23.0
48404850
This argument will be removed in a future version, replaced
48414851
by result_type='broadcast'.
48424852
48434853
raw : boolean, default False
4844-
If False, convert each row or column into a Series. If raw=True the
4845-
passed function will receive ndarray objects instead. If you are
4846-
just applying a NumPy reduction function this will achieve much
4847-
better performance
4854+
* `False` : passes each row or column into a `Series` to the
4855+
function.
4856+
* `True` : the passed function will receive ndarray objects
4857+
instead.
4858+
If you are just applying a NumPy reduction function this will
4859+
achieve much better performance.
48484860
reduce : boolean or None, default None
4849-
Try to apply reduction procedures. If the DataFrame is empty,
4850-
apply will use reduce to determine whether the result should be a
4851-
Series or a DataFrame. If reduce is None (the default), apply's
4852-
return value will be guessed by calling func an empty Series (note:
4853-
while guessing, exceptions raised by func will be ignored). If
4854-
reduce is True a Series will always be returned, and if False a
4855-
DataFrame will always be returned.
4856-
4857-
.. deprecated:: 0.23.0
4861+
Try to apply reduction procedures. If the `DataFrame` is empty,
4862+
:meth:`apply` will use reduce to determine whether the result
4863+
should be a `Series` or a `DataFrame`. If reduce is None (the
4864+
default), :meth:`apply`'s return value will be guessed by calling
4865+
func on an empty `Series`
4866+
(note: while guessing, exceptions raised by `func` will be
4867+
ignored).
4868+
If reduce is True a `Series` will always be returned, and if
4869+
`False` a `DataFrame` will always be returned.
4870+
4871+
.. deprecated:: 0.23.0.
48584872
This argument will be removed in a future version, replaced
48594873
by result_type='reduce'.
48604874
4861-
result_type : {'expand', 'reduce', 'broadcast, None}
4862-
These only act when axis=1 {columns}:
4875+
result_type : {'expand', 'reduce', 'broadcast', None}
4876+
These only act when `axis=1` (columns):
48634877
48644878
* 'expand' : list-like results will be turned into columns.
4865-
* 'reduce' : return a Series if possible rather than expanding
4866-
list-like results. This is the opposite to 'expand'.
4879+
* 'reduce' : returns a `Series` if possible rather than expanding
4880+
list-like results. This is the opposite of 'expand'.
48674881
* 'broadcast' : results will be broadcast to the original shape
4868-
of the frame, the original index & columns will be retained.
4882+
of the `DataFrame`, the original index and columns will be
4883+
retained.
48694884
4870-
The default behaviour (None) depends on the return value of the
4871-
applied function: list-like results will be returned as a Series
4872-
of those. However if the apply function returns a Series these
4885+
The default behaviour (`None`) depends on the return value of the
4886+
applied function: list-like results will be returned as a `Series`
4887+
of those. However if the apply function returns a `Series` these
48734888
are expanded to columns.
48744889
4875-
.. versionadded:: 0.23.0
4890+
.. versionadded:: 0.23.0.
48764891
48774892
args : tuple
48784893
Positional arguments to pass to function in addition to the
4879-
array/series
4880-
Additional keyword arguments will be passed as keywords to the function
4894+
array/series.
4895+
kwds :
4896+
Additional keyword arguments to pass as keywords to the function.
48814897
48824898
Notes
48834899
-----
@@ -4941,6 +4957,7 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None,
49414957
3 [1, 2]
49424958
4 [1, 2]
49434959
5 [1, 2]
4960+
dtype: object
49444961
49454962
Passing result_type='expand' will expand list-like results
49464963
to columns of a Dataframe
@@ -4958,7 +4975,7 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None,
49584975
``result_type='expand'``. The resulting column names
49594976
will be the Series index.
49604977
4961-
>>> df.apply(lambda x: Series([1, 2], index=['foo', 'bar']), axis=1)
4978+
>>> df.apply(lambda x: pd.Series([1, 2], index=['foo', 'bar']), axis=1)
49624979
foo bar
49634980
0 1 2
49644981
1 1 2

0 commit comments

Comments
 (0)