@@ -4362,8 +4362,9 @@ def replace(self, to_replace=None, value=None, inplace=False, limit=None,
4362
4362
----------
4363
4363
to_replace : str, regex, list, dict, Series, numeric, or None
4364
4364
4365
- * str or regex:
4366
-
4365
+ * numeric, str or regex:
4366
+ - numeric: numeric values equal to `to_replace` will be
4367
+ replaced with `value`
4367
4368
- str: string exactly matching `to_replace` will be replaced
4368
4369
with `value`
4369
4370
- regex: regexs matching `to_replace` will be replaced with
@@ -4381,14 +4382,16 @@ def replace(self, to_replace=None, value=None, inplace=False, limit=None,
4381
4382
4382
4383
* dict:
4383
4384
4385
+ - Keys map to column names and values map to substitution
4386
+ values. For example, {'a': 1, 'b': 'z'} looks for the value 1
4387
+ in column 'a' and the value 'z' in column 'b'. You can treat
4388
+ this as a special case of passing two lists except that you
4389
+ are specifying the column to search in.
4384
4390
- Nested dictionaries, e.g., {'a': {'b': nan}}, are read as
4385
4391
follows: look in column 'a' for the value 'b' and replace it
4386
- with nan . You can nest regular expressions as well. Note that
4392
+ with NaN . You can nest regular expressions as well. Note that
4387
4393
column names (the top-level dictionary keys in a nested
4388
4394
dictionary) **cannot** be regular expressions.
4389
- - Keys map to column names and values map to substitution
4390
- values. You can treat this as a special case of passing two
4391
- lists except that you are specifying the column to search in.
4392
4395
4393
4396
* None:
4394
4397
@@ -4421,13 +4424,12 @@ def replace(self, to_replace=None, value=None, inplace=False, limit=None,
4421
4424
4422
4425
See Also
4423
4426
--------
4424
- NDFrame.reindex
4425
- NDFrame.asfreq
4426
- NDFrame.fillna
4427
+ :func:`DataFrame.fillna` : Fill NA/NaN values
4428
+ :func:`DataFrame.where` : Replace values based on boolean condition
4427
4429
4428
4430
Returns
4429
4431
-------
4430
- filled : NDFrame
4432
+ filled : DataFrame
4431
4433
4432
4434
Raises
4433
4435
------
@@ -4438,6 +4440,9 @@ def replace(self, to_replace=None, value=None, inplace=False, limit=None,
4438
4440
``dict``, ``ndarray``, or ``Series``
4439
4441
* If `to_replace` is ``None`` and `regex` is not compilable into a
4440
4442
regular expression or is a list, dict, ndarray, or Series.
4443
+ * When replacing multiple `bool` or `datetime64` objects and the
4444
+ the arguments to `to_replace` does not match the type of the
4445
+ value being replaced
4441
4446
ValueError
4442
4447
* If `to_replace` and `value` are ``list`` s or ``ndarray`` s, but
4443
4448
they are not the same length.
@@ -4453,6 +4458,63 @@ def replace(self, to_replace=None, value=None, inplace=False, limit=None,
4453
4458
*are* strings, then you can do this.
4454
4459
* This method has *a lot* of options. You are encouraged to experiment
4455
4460
and play with this method to gain intuition about how it works.
4461
+
4462
+ Examples
4463
+ --------
4464
+ Simple Cases
4465
+
4466
+ Scalar Value
4467
+
4468
+ >>> df = pd.DataFrame({'A': [0, 1, 2, 3, 4],
4469
+ ... 'B': [5, 6, 7, 8, 9],
4470
+ ... 'C': ['a', 'b', 'c', 'd', 'e']})
4471
+ >>> df.replace(0, 5)
4472
+ A B C
4473
+ 0 5 5 a
4474
+ 1 1 6 b
4475
+ 2 2 7 c
4476
+ 3 3 8 d
4477
+ 4 4 9 e
4478
+
4479
+ Lists
4480
+
4481
+ >>> df.replace([0, 1, 2, 3, 4], [4, 3, 2, 1, 0])
4482
+ A B C
4483
+ 0 4 5 a
4484
+ 1 3 6 b
4485
+ 2 2 7 c
4486
+ 3 1 8 d
4487
+ 4 0 9 e
4488
+
4489
+ Simple Dict
4490
+
4491
+ >>> df.replace({0: 10, 1: 100})
4492
+ A B C
4493
+ 0 10 5 a
4494
+ 1 100 6 b
4495
+ 2 2 7 c
4496
+ 3 3 8 d
4497
+ 4 4 9 e
4498
+
4499
+ Dict for values by column
4500
+
4501
+ >>> df.replace({'A': 0, 'B': 5}, 100)
4502
+ A B C
4503
+ 0 100 100 a
4504
+ 1 1 6 b
4505
+ 2 2 7 c
4506
+ 3 3 8 d
4507
+ 4 4 9 e
4508
+
4509
+ regex
4510
+
4511
+ >>> df = {'a': list(range(4)), 'b': list('ab..'), 'c': ['a', 'b', np.nan, 'd']}
4512
+ >>> df.replace(r'\s*\.\s*', np.nan, regex=True)
4513
+ a b c
4514
+ 0 0 a a
4515
+ 1 1 b b
4516
+ 2 2 NaN NaN
4517
+ 3 3 NaN d
4456
4518
4457
4519
"""
4458
4520
inplace = validate_bool_kwarg (inplace , 'inplace' )
0 commit comments