@@ -4818,66 +4818,82 @@ def aggregate(self, func, axis=0, *args, **kwargs):
4818
4818
4819
4819
def apply (self , func , axis = 0 , broadcast = None , raw = False , reduce = None ,
4820
4820
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`.
4822
4823
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.
4827
4830
4828
4831
Parameters
4829
4832
----------
4830
4833
func : function
4831
- Function to apply to each column/ row
4834
+ Function to apply to each column or row.
4832
4835
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.
4835
4840
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.
4838
4848
4839
4849
.. deprecated:: 0.23.0
4840
4850
This argument will be removed in a future version, replaced
4841
4851
by result_type='broadcast'.
4842
4852
4843
4853
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.
4848
4860
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.
4858
4872
This argument will be removed in a future version, replaced
4859
4873
by result_type='reduce'.
4860
4874
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) :
4863
4877
4864
4878
* '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'.
4867
4881
* '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.
4869
4884
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
4873
4888
are expanded to columns.
4874
4889
4875
- .. versionadded:: 0.23.0
4890
+ .. versionadded:: 0.23.0.
4876
4891
4877
4892
args : tuple
4878
4893
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.
4881
4897
4882
4898
Notes
4883
4899
-----
@@ -4941,6 +4957,7 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None,
4941
4957
3 [1, 2]
4942
4958
4 [1, 2]
4943
4959
5 [1, 2]
4960
+ dtype: object
4944
4961
4945
4962
Passing result_type='expand' will expand list-like results
4946
4963
to columns of a Dataframe
@@ -4958,7 +4975,7 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None,
4958
4975
``result_type='expand'``. The resulting column names
4959
4976
will be the Series index.
4960
4977
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)
4962
4979
foo bar
4963
4980
0 1 2
4964
4981
1 1 2
0 commit comments