Replace values given in `to_replace` with `value`. Values of the Series/DataFrame are replaced with other values dynamically. This differs from updating with ``.loc`` or ``.iloc``, which require you to specify a location to update with some value. Parameter
(
self,
to_replace=None,
value=lib.no_default,
*,
inplace: bool = False,
regex: bool = False,
)
| 7374 | |
| 7375 | @final |
| 7376 | def replace( |
| 7377 | self, |
| 7378 | to_replace=None, |
| 7379 | value=lib.no_default, |
| 7380 | *, |
| 7381 | inplace: bool = False, |
| 7382 | regex: bool = False, |
| 7383 | ) -> Self: |
| 7384 | """ |
| 7385 | Replace values given in `to_replace` with `value`. |
| 7386 | |
| 7387 | Values of the Series/DataFrame are replaced with other values dynamically. |
| 7388 | This differs from updating with ``.loc`` or ``.iloc``, which require |
| 7389 | you to specify a location to update with some value. |
| 7390 | |
| 7391 | Parameters |
| 7392 | ---------- |
| 7393 | to_replace : str, regex, list, dict, Series, int, float, or None |
| 7394 | How to find the values that will be replaced. |
| 7395 | |
| 7396 | * numeric, str or regex: |
| 7397 | |
| 7398 | - numeric: numeric values equal to `to_replace` will be |
| 7399 | replaced with `value` |
| 7400 | - str: string exactly matching `to_replace` will be replaced |
| 7401 | with `value` |
| 7402 | - regex: regexes matching `to_replace` will be replaced with |
| 7403 | `value` |
| 7404 | |
| 7405 | * list of str, regex, or numeric: |
| 7406 | |
| 7407 | - First, if `to_replace` and `value` are both lists, they |
| 7408 | **must** be the same length. |
| 7409 | - Second, if ``regex=True`` then all of the strings in **both** |
| 7410 | lists will be interpreted as regexes otherwise they will match |
| 7411 | directly. This doesn't matter much for `value` since there |
| 7412 | are only a few possible substitution regexes you can use. |
| 7413 | - str, regex and numeric rules apply as above. |
| 7414 | |
| 7415 | * dict: |
| 7416 | |
| 7417 | - Dicts can be used to specify different replacement values |
| 7418 | for different existing values. For example, |
| 7419 | ``{'a': 'b', 'y': 'z'}`` replaces the value 'a' with 'b' and |
| 7420 | 'y' with 'z'. To use a dict in this way, the optional `value` |
| 7421 | parameter should not be given. |
| 7422 | - For a DataFrame a dict can specify that different values |
| 7423 | should be replaced in different columns. For example, |
| 7424 | ``{'a': 1, 'b': 'z'}`` looks for the value 1 in column 'a' |
| 7425 | and the value 'z' in column 'b' and replaces these values |
| 7426 | with whatever is specified in `value`. The `value` parameter |
| 7427 | should not be ``None`` in this case. You can treat this as a |
| 7428 | special case of passing two lists except that you are |
| 7429 | specifying the column to search in. |
| 7430 | - For a DataFrame nested dictionaries, e.g., |
| 7431 | ``{'a': {'b': np.nan}}``, are read as follows: look in column |
| 7432 | 'a' for the value 'b' and replace it with NaN. The optional `value` |
| 7433 | parameter should not be specified to use a nested dict in this |