@@ -5563,39 +5563,52 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None,
5563
5563
5564
5564
def applymap (self , func ):
5565
5565
"""
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.
5569
5570
5570
5571
Parameters
5571
5572
----------
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.
5590
5575
5591
5576
Returns
5592
5577
-------
5593
- applied : DataFrame
5578
+ DataFrame
5579
+ Transformed DataFrame.
5594
5580
5595
5581
See also
5596
5582
--------
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.
5598
5607
5608
+ >>> df ** 2
5609
+ 0 1
5610
+ 0 1.000000 4.494400
5611
+ 1 11.262736 20.857489
5599
5612
"""
5600
5613
5601
5614
# if we have a dtype == 'M8[ns]', provide boxed values
0 commit comments