|
414 | 414 | * numeric, str or regex:
|
415 | 415 |
|
416 | 416 | - numeric: numeric values equal to `to_replace` will be
|
417 |
| - replaced with `value` |
| 417 | + replaced with `value` |
418 | 418 | - str: string exactly matching `to_replace` will be replaced
|
419 |
| - with `value` |
| 419 | + with `value` |
420 | 420 | - regex: regexs matching `to_replace` will be replaced with
|
421 |
| - `value` |
| 421 | + `value` |
422 | 422 |
|
423 | 423 | * list of str, regex, or numeric:
|
424 | 424 |
|
425 | 425 | - First, if `to_replace` and `value` are both lists, they
|
426 |
| - **must** be the same length. |
| 426 | + **must** be the same length. |
427 | 427 | - Second, if ``regex=True`` then all of the strings in **both**
|
428 |
| - lists will be interpreted as regexs otherwise they will match |
429 |
| - directly. This doesn't matter much for `value` since there |
430 |
| - are only a few possible substitution regexes you can use. |
| 428 | + lists will be interpreted as regexs otherwise they will match |
| 429 | + directly. This doesn't matter much for `value` since there |
| 430 | + are only a few possible substitution regexes you can use. |
431 | 431 | - str, regex and numeric rules apply as above.
|
432 | 432 |
|
433 | 433 | * dict:
|
434 | 434 |
|
435 | 435 | - Dicts can be used to specify different replacement values
|
436 |
| - for different existing values. For example, |
437 |
| - ``{{'a': 'b', 'y': 'z'}}`` replaces the value 'a' with 'b' and |
438 |
| - 'y' with 'z'. To use a dict in this way the `value` |
439 |
| - parameter should be `None`. |
| 436 | + for different existing values. For example, |
| 437 | + ``{{'a': 'b', 'y': 'z'}}`` replaces the value 'a' with 'b' and |
| 438 | + 'y' with 'z'. To use a dict in this way the `value` |
| 439 | + parameter should be `None`. |
440 | 440 | - For a DataFrame a dict can specify that different values
|
441 |
| - should be replaced in different columns. For example, |
442 |
| - ``{{'a': 1, 'b': 'z'}}`` looks for the value 1 in column 'a' |
443 |
| - and the value 'z' in column 'b' and replaces these values |
444 |
| - with whatever is specified in `value`. The `value` parameter |
445 |
| - should not be ``None`` in this case. You can treat this as a |
446 |
| - special case of passing two lists except that you are |
447 |
| - specifying the column to search in. |
| 441 | + should be replaced in different columns. For example, |
| 442 | + ``{{'a': 1, 'b': 'z'}}`` looks for the value 1 in column 'a' |
| 443 | + and the value 'z' in column 'b' and replaces these values |
| 444 | + with whatever is specified in `value`. The `value` parameter |
| 445 | + should not be ``None`` in this case. You can treat this as a |
| 446 | + special case of passing two lists except that you are |
| 447 | + specifying the column to search in. |
448 | 448 | - For a DataFrame nested dictionaries, e.g.,
|
449 |
| - ``{{'a': {{'b': np.nan}}}}``, are read as follows: look in column |
450 |
| - 'a' for the value 'b' and replace it with NaN. The `value` |
451 |
| - parameter should be ``None`` to use a nested dict in this |
452 |
| - way. You can nest regular expressions as well. Note that |
453 |
| - column names (the top-level dictionary keys in a nested |
454 |
| - dictionary) **cannot** be regular expressions. |
| 449 | + ``{{'a': {{'b': np.nan}}}}``, are read as follows: look in column |
| 450 | + 'a' for the value 'b' and replace it with NaN. The `value` |
| 451 | + parameter should be ``None`` to use a nested dict in this |
| 452 | + way. You can nest regular expressions as well. Note that |
| 453 | + column names (the top-level dictionary keys in a nested |
| 454 | + dictionary) **cannot** be regular expressions. |
455 | 455 |
|
456 | 456 | * None:
|
457 | 457 |
|
458 | 458 | - This means that the `regex` argument must be a string,
|
459 |
| - compiled regular expression, or list, dict, ndarray or |
460 |
| - Series of such elements. If `value` is also ``None`` then |
461 |
| - this **must** be a nested dictionary or Series. |
| 459 | + compiled regular expression, or list, dict, ndarray or |
| 460 | + Series of such elements. If `value` is also ``None`` then |
| 461 | + this **must** be a nested dictionary or Series. |
462 | 462 |
|
463 | 463 | See the examples section for examples of each of these.
|
464 | 464 | value : scalar, dict, list, str, regex, default None
|
|
497 | 497 | TypeError
|
498 | 498 | * If `to_replace` is not a scalar, array-like, ``dict``, or ``None``
|
499 | 499 | * If `to_replace` is a ``dict`` and `value` is not a ``list``,
|
500 |
| - ``dict``, ``ndarray``, or ``Series`` |
| 500 | + ``dict``, ``ndarray``, or ``Series`` |
501 | 501 | * If `to_replace` is ``None`` and `regex` is not compilable
|
502 |
| - into a regular expression or is a list, dict, ndarray, or |
503 |
| - Series. |
| 502 | + into a regular expression or is a list, dict, ndarray, or |
| 503 | + Series. |
504 | 504 | * When replacing multiple ``bool`` or ``datetime64`` objects and
|
505 |
| - the arguments to `to_replace` does not match the type of the |
506 |
| - value being replaced |
| 505 | + the arguments to `to_replace` does not match the type of the |
| 506 | + value being replaced |
507 | 507 |
|
508 | 508 | ValueError
|
509 | 509 | * If a ``list`` or an ``ndarray`` is passed to `to_replace` and
|
510 |
| - `value` but they are not the same length. |
| 510 | + `value` but they are not the same length. |
511 | 511 |
|
512 | 512 | See Also
|
513 | 513 | --------
|
|
518 | 518 | Notes
|
519 | 519 | -----
|
520 | 520 | * Regex substitution is performed under the hood with ``re.sub``. The
|
521 |
| - rules for substitution for ``re.sub`` are the same. |
| 521 | + rules for substitution for ``re.sub`` are the same. |
522 | 522 | * Regular expressions will only substitute on strings, meaning you
|
523 |
| - cannot provide, for example, a regular expression matching floating |
524 |
| - point numbers and expect the columns in your frame that have a |
525 |
| - numeric dtype to be matched. However, if those floating point |
526 |
| - numbers *are* strings, then you can do this. |
| 523 | + cannot provide, for example, a regular expression matching floating |
| 524 | + point numbers and expect the columns in your frame that have a |
| 525 | + numeric dtype to be matched. However, if those floating point |
| 526 | + numbers *are* strings, then you can do this. |
527 | 527 | * This method has *a lot* of options. You are encouraged to experiment
|
528 |
| - and play with this method to gain intuition about how it works. |
| 528 | + and play with this method to gain intuition about how it works. |
529 | 529 | * When dict is used as the `to_replace` value, it is like
|
530 |
| - key(s) in the dict are the to_replace part and |
531 |
| - value(s) in the dict are the value parameter. |
| 530 | + key(s) in the dict are the to_replace part and |
| 531 | + value(s) in the dict are the value parameter. |
532 | 532 |
|
533 | 533 | Examples
|
534 | 534 | --------
|
|
0 commit comments