Skip to content

Commit 3561580

Browse files
oreretaTomAugspurger
authored andcommitted
DOC: update the DataFrame.applymap docstring (#20136)
1 parent fb51ca9 commit 3561580

File tree

1 file changed

+36
-23
lines changed

1 file changed

+36
-23
lines changed

pandas/core/frame.py

+36-23
Original file line numberDiff line numberDiff line change
@@ -5563,39 +5563,52 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None,
55635563

55645564
def applymap(self, func):
55655565
"""
5566-
Apply a function to a DataFrame that is intended to operate
5567-
elementwise, i.e. like doing map(func, series) for each series in the
5568-
DataFrame
5566+
Apply a function to a Dataframe elementwise.
5567+
5568+
This method applies a function that accepts and returns a scalar
5569+
to every element of a DataFrame.
55695570
55705571
Parameters
55715572
----------
5572-
func : function
5573-
Python function, returns a single value from a single value
5574-
5575-
Examples
5576-
--------
5577-
5578-
>>> df = pd.DataFrame(np.random.randn(3, 3))
5579-
>>> df
5580-
0 1 2
5581-
0 -0.029638 1.081563 1.280300
5582-
1 0.647747 0.831136 -1.549481
5583-
2 0.513416 -0.884417 0.195343
5584-
>>> df = df.applymap(lambda x: '%.2f' % x)
5585-
>>> df
5586-
0 1 2
5587-
0 -0.03 1.08 1.28
5588-
1 0.65 0.83 -1.55
5589-
2 0.51 -0.88 0.20
5573+
func : callable
5574+
Python function, returns a single value from a single value.
55905575
55915576
Returns
55925577
-------
5593-
applied : DataFrame
5578+
DataFrame
5579+
Transformed DataFrame.
55945580
55955581
See also
55965582
--------
5597-
DataFrame.apply : For operations on rows/columns
5583+
DataFrame.apply : Apply a function along input axis of DataFrame
5584+
5585+
Examples
5586+
--------
5587+
>>> df = pd.DataFrame([[1, 2.12], [3.356, 4.567]])
5588+
>>> df
5589+
0 1
5590+
0 1.000 2.120
5591+
1 3.356 4.567
5592+
5593+
>>> df.applymap(lambda x: len(str(x)))
5594+
0 1
5595+
0 3 4
5596+
1 5 5
5597+
5598+
Note that a vectorized version of `func` often exists, which will
5599+
be much faster. You could square each number elementwise.
5600+
5601+
>>> df.applymap(lambda x: x**2)
5602+
0 1
5603+
0 1.000000 4.494400
5604+
1 11.262736 20.857489
5605+
5606+
But it's better to avoid applymap in that case.
55985607
5608+
>>> df ** 2
5609+
0 1
5610+
0 1.000000 4.494400
5611+
1 11.262736 20.857489
55995612
"""
56005613

56015614
# if we have a dtype == 'M8[ns]', provide boxed values

0 commit comments

Comments
 (0)