Skip to content

Commit d52ac81

Browse files
author
mdeboc
committed
DOC: update the pandas.DataFrame.apply docstring
1 parent cf2f2d0 commit d52ac81

File tree

1 file changed

+25
-44
lines changed

1 file changed

+25
-44
lines changed

pandas/core/frame.py

Lines changed: 25 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4906,46 +4906,33 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None,
49064906
Examples
49074907
--------
49084908
4909-
We use this DataFrame to illustrate
4910-
4911-
>>> df = pd.DataFrame(np.tile(np.arange(3), 6).reshape(6, -1) + 1,
4912-
... columns=['A', 'B', 'C'])
4909+
>>> df = pd.DataFrame([[4, 9],] * 3, columns=['A', 'B'])
49134910
>>> df
4914-
A B C
4915-
0 1 2 3
4916-
1 1 2 3
4917-
2 1 2 3
4918-
3 1 2 3
4919-
4 1 2 3
4920-
5 1 2 3
4911+
A B
4912+
0 4 9
4913+
1 4 9
4914+
2 4 9
49214915
49224916
Using a numpy universal function (in this case the same as
49234917
``np.sqrt(df)``):
49244918
49254919
>>> df.apply(np.sqrt)
4926-
A B C
4927-
0 1.0 1.414214 1.732051
4928-
1 1.0 1.414214 1.732051
4929-
2 1.0 1.414214 1.732051
4930-
3 1.0 1.414214 1.732051
4931-
4 1.0 1.414214 1.732051
4932-
5 1.0 1.414214 1.732051
4920+
A B
4921+
0 2.0 3.0
4922+
1 2.0 3.0
4923+
2 2.0 3.0
49334924
49344925
Using a reducing function on either axis
49354926
49364927
>>> df.apply(np.sum, axis=0)
4937-
A 6
4938-
B 12
4939-
C 18
4928+
A 12
4929+
B 27
49404930
dtype: int64
49414931
49424932
>>> df.apply(np.sum, axis=1)
4943-
0 6
4944-
1 6
4945-
2 6
4946-
3 6
4947-
4 6
4948-
5 6
4933+
0 13
4934+
1 13
4935+
2 13
49494936
dtype: int64
49504937
49514938
Retuning a list-like will result in a Series
@@ -4954,9 +4941,12 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None,
49544941
0 [1, 2]
49554942
1 [1, 2]
49564943
2 [1, 2]
4957-
3 [1, 2]
4958-
4 [1, 2]
4959-
5 [1, 2]
4944+
dtype: object
4945+
4946+
>>> df.apply(lambda x: [1,], axis=1)
4947+
0 [1]
4948+
1 [1]
4949+
2 [1]
49604950
dtype: object
49614951
49624952
Passing result_type='expand' will expand list-like results
@@ -4967,9 +4957,6 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None,
49674957
0 1 2
49684958
1 1 2
49694959
2 1 2
4970-
3 1 2
4971-
4 1 2
4972-
5 1 2
49734960
49744961
Returning a Series inside the function is similar to passing
49754962
``result_type='expand'``. The resulting column names
@@ -4980,23 +4967,17 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None,
49804967
0 1 2
49814968
1 1 2
49824969
2 1 2
4983-
3 1 2
4984-
4 1 2
4985-
5 1 2
49864970
49874971
Passing ``result_type='broadcast'`` will ensure the same shape
49884972
result, whether list-like or scalar is returned by the function,
49894973
and broadcast it along the axis. The resulting column names will
49904974
be the originals.
49914975
4992-
>>> df.apply(lambda x: [1, 2, 3], axis=1, result_type='broadcast')
4993-
A B C
4994-
0 1 2 3
4995-
1 1 2 3
4996-
2 1 2 3
4997-
3 1 2 3
4998-
4 1 2 3
4999-
5 1 2 3
4976+
>>> df.apply(lambda x: [1, 2], axis=1, result_type='broadcast')
4977+
A B
4978+
0 1 2
4979+
1 1 2
4980+
2 1 2
50004981
50014982
See also
50024983
--------

0 commit comments

Comments
 (0)