|
570 | 570 | .. deprecated:: 2.1.0
|
571 | 571 | regex : bool or same types as `to_replace`, default False
|
572 | 572 | Whether to interpret `to_replace` and/or `value` as regular
|
573 |
| - expressions. If this is ``True`` then `to_replace` *must* be a |
574 |
| - string. Alternatively, this could be a regular expression or a |
| 573 | + expressions. Alternatively, this could be a regular expression or a |
575 | 574 | list, dict, or array of regular expressions in which case
|
576 | 575 | `to_replace` must be ``None``.
|
577 | 576 | method : {{'pad', 'ffill', 'bfill'}}
|
|
790 | 789 |
|
791 | 790 | .. versionchanged:: 1.4.0
|
792 | 791 | Previously the explicit ``None`` was silently ignored.
|
| 792 | +
|
| 793 | + When ``regex=True``, ``value`` is not ``None`` and `to_replace` is a string, |
| 794 | + the replacement will be applied in all columns of the DataFrame. |
| 795 | +
|
| 796 | + >>> df = pd.DataFrame({{'A': [0, 1, 2, 3, 4], |
| 797 | + ... 'B': ['a', 'b', 'c', 'd', 'e'], |
| 798 | + ... 'C': ['f', 'g', 'h', 'i', 'j']}}) |
| 799 | +
|
| 800 | + >>> df.replace(to_replace='^[a-g]', value = 'e', regex=True) |
| 801 | + A B C |
| 802 | + 0 0 e e |
| 803 | + 1 1 e e |
| 804 | + 2 2 e h |
| 805 | + 3 3 e i |
| 806 | + 4 4 e j |
| 807 | +
|
| 808 | + If ``value`` is not ``None`` and `to_replace` is a dictionary, the dictionary |
| 809 | + keys will be the DataFrame columns that the replacement will be applied. |
| 810 | +
|
| 811 | + >>> df.replace(to_replace={{'B': '^[a-c]', 'C': '^[h-j]'}}, value = 'e', regex=True) |
| 812 | + A B C |
| 813 | + 0 0 e f |
| 814 | + 1 1 e g |
| 815 | + 2 2 e e |
| 816 | + 3 3 d e |
| 817 | + 4 4 e e |
793 | 818 | """
|
794 | 819 |
|
795 | 820 | _shared_docs[
|
|
0 commit comments