pandas.DataFrame.replace#

DataFrame.replace(to_replace=None, value=_NoDefault.no_default, *, inplace=False, limit=None, regex=False, method=_NoDefault.no_default)[源代码]#

value 替换 to_replace 中给定的值。

动态地将 Series/DataFrame 的值替换为其他值。这与使用 .loc.iloc 进行更新不同,后者要求您指定一个位置来用某个值进行更新。

Parameters:
to_replace字符串、正则表达式、列表、字典、Series、整数、浮点数或 None

如何查找将被替换的值。

  • 数值、字符串或正则表达式:

    • numeric:等于 to_replace 的数值将被替换为 value

    • str:完全匹配 to_replace 的字符串将被替换为 value

    • regex:匹配 to_replace 的正则表达式将被替换为 value

  • 字符串列表、正则表达式或数值:

    • 首先,如果 to_replacevalue 都是列表,它们**必须**具有相同的长度。

    • 其次,如果 regex=True,那么**两个**列表中的所有字符串都将被解释为正则表达式,否则它们将直接匹配。这对于 ``value` 来说差别不大,因为只有少数几个可能的替换正则表达式可以使用。

    • str, regex and numeric rules apply as above.

  • 字典:

    • 字典可以用来为不同的现有值指定不同的替换值。例如,{'a': 'b', 'y': 'z'} 会将值 ‘a’ 替换为 ‘b’,将 ‘y’ 替换为 ‘z’。要这样使用字典,可选的 value 参数不应给出。

    • 对于 DataFrame,字典可以指定在不同列中替换不同的值。例如,{'a': 1, 'b': 'z'} 会在列 ‘a’ 中查找值 1,在列 ‘b’ 中查找值 ‘z’,并将这些值替换为 value 中指定的任何值。在这种情况下,value 参数不应为 None。你可以将其视为传递两个列表的特殊情况,只不过你指定了要搜索的列。

    • 对于 DataFrame,嵌套字典,例如 {'a': {'b': np.nan}},按如下方式读取:在列 ‘a’ 中查找值 ‘b’ 并将其替换为 NaN。要以这种方式使用嵌套字典,不应指定可选的 value 参数。你也可以嵌套正则表达式。请注意,列名(嵌套字典中的顶层字典键)**不能**是正则表达式。

  • None:

    • 这意味着 regex 参数必须是字符串、编译的正则表达式,或者包含这些元素的列表、字典、ndarray 或 Series。如果 value 也为 None,则这**必须**是一个嵌套字典或 Series。

请参阅示例部分,其中包含上述各种情况的示例。

value标量、字典、列表、字符串、正则表达式,默认为 None

用于替换与 to_replace 匹配的任何值的目标值。对于 DataFrame,可以使用字典值来指定为每列使用哪个值(字典中不存在的列将不会被填充)。也允许使用正则表达式、字符串以及这些对象的列表或字典。

inplacebool,默认 False

如果为 True,则原地执行操作并返回 None。

<strong>limit</strong>int,默认 None

向前或向后填充的最大间隔大小。

自 2.1.0 版本弃用.

regex : bool 或与 to_replace 相同的类型,默认为 Falsebool 或与…

是否将 to_replace 和/或 value 解释为正则表达式。或者,这可以是一个正则表达式,也可以是正则表达式的列表、字典或数组,在这种情况下 to_replace 必须为 None

<strong>method</strong>{‘pad’, ‘ffill’, ‘bfill’}

to_replace 是标量、列表或元组而 valueNone 时,用于替换的方法。

自 2.1.0 版本弃用.

Returns:
Series/DataFrame

替换后的对象。

Raises:
AssertionError
  • 如果 regex 不是 boolto_replace 不是 None

TypeError
  • 如果 to_replace 不是标量、类数组、dictNone

  • 如果 to_replacedictvalue 不是 listdictndarraySeries

  • 如果 to_replaceNoneregex 无法编译为正则表达式,或者是一个列表、字典、ndarray 或 Series。

  • 当替换多个 booldatetime64 对象,并且 to_replace 的参数与被替换值的类型不匹配时

ValueError
  • 如果将 listndarray 传递给 to_replacevalue,但它们的长度不相等。

参见

Series.fillna

填充 NA 值。

DataFrame.fillna

填充 NA 值。

Series.where

根据布尔条件替换值。

DataFrame.where

根据布尔条件替换值。

DataFrame.map

将函数逐个元素应用到 DataFrame。

Series.map

根据输入映射或函数映射 Series 的值。

Series.str.replace

简单的字符串替换。

Notes

  • 正则表达式替换是在后台使用 re.sub 执行的。re.sub 的替换规则相同。

  • 正则表达式仅在字符串上进行替换,这意味着你不能提供一个匹配浮点数的正则表达式,并期望你的 DataFrame 中具有数值 dtype 的列能够被匹配。然而,如果这些浮点数是*字符串*,那么你可以这样做。

  • 此方法有*很多*选项。鼓励你进行实验和尝试,以直观地了解它的工作原理。

  • 当字典用作 to_replace 值时,字典中的键是 to_replace 部分,字典中的值是 value 参数。

Examples

标量 `to_replace` 和 `value`

>>> s = pd.Series([1, 2, 3, 4, 5])
>>> s.replace(1, 5)
0    5
1    2
2    3
3    4
4    5
dtype: int64
>>> df = pd.DataFrame({'A': [0, 1, 2, 3, 4],
...                    'B': [5, 6, 7, 8, 9],
...                    'C': ['a', 'b', 'c', 'd', 'e']})
>>> df.replace(0, 5)
    A  B  C
0  5  5  a
1  1  6  b
2  2  7  c
3  3  8  d
4  4  9  e

类列表 `to_replace`

>>> df.replace([0, 1, 2, 3], 4)
    A  B  C
0  4  5  a
1  4  6  b
2  4  7  c
3  4  8  d
4  4  9  e
>>> df.replace([0, 1, 2, 3], [4, 3, 2, 1])
    A  B  C
0  4  5  a
1  3  6  b
2  2  7  c
3  1  8  d
4  4  9  e
>>> s.replace([1, 2], method='bfill')
0    3
1    3
2    3
3    4
4    5
dtype: int64

类字典 `to_replace`

>>> df.replace({0: 10, 1: 100})
        A  B  C
0   10  5  a
1  100  6  b
2    2  7  c
3    3  8  d
4    4  9  e
>>> df.replace({'A': 0, 'B': 5}, 100)
        A    B  C
0  100  100  a
1    1    6  b
2    2    7  c
3    3    8  d
4    4    9  e
>>> df.replace({'A': {0: 100, 4: 400}})
        A  B  C
0  100  5  a
1    1  6  b
2    2  7  c
3    3  8  d
4  400  9  e

正则表达式 `to_replace`

>>> df = pd.DataFrame({'A': ['bat', 'foo', 'bait'],
...                    'B': ['abc', 'bar', 'xyz']})
>>> df.replace(to_replace=r'^ba.$', value='new', regex=True)
        A    B
0   new  abc
1   foo  new
2  bait  xyz
>>> df.replace({'A': r'^ba.$'}, {'A': 'new'}, regex=True)
        A    B
0   new  abc
1   foo  bar
2  bait  xyz
>>> df.replace(regex=r'^ba.$', value='new')
        A    B
0   new  abc
1   foo  new
2  bait  xyz
>>> df.replace(regex={r'^ba.$': 'new', 'foo': 'xyz'})
        A    B
0   new  abc
1   xyz  new
2  bait  xyz
>>> df.replace(regex=[r'^ba.$', 'foo'], value='new')
        A    B
0   new  abc
1   new  new
2  bait  xyz

比较 s.replace({'a': None})s.replace('a', None) 的行为,以理解 to_replace 参数的独特性:

>>> s = pd.Series([10, 'a', 'a', 'b', 'a'])

当使用 dict 作为 to_replace 值时,其效果类似于 dict 中的值等于 value 参数。s.replace({'a': None}) 等同于 s.replace(to_replace={'a': None}, value=None, method=None)

>>> s.replace({'a': None})
0      10
1    None
2    None
3       b
4    None
dtype: object

当未显式传递 valueto_replace 是标量、列表或元组时,replace 使用 method 参数(默认为 ‘pad’)进行替换。因此,在这种情况下,第 1 行和第 2 行中的 ‘a’ 值被替换为 10,第 4 行中的 ‘b’ 值被替换为 10。

>>> s.replace('a')
0    10
1    10
2    10
3     b
4     b
dtype: object

自 2.1.0 版本弃用: ‘method’ 参数和填充行为已弃用。

另一方面,如果为 value 显式传递了 None,则会得到保留:

>>> s.replace('a', None)
0      10
1    None
2    None
3       b
4    None
dtype: object

在 1.4.0 版本发生变更: 此前显式的 None 会被默默忽略。

regex=Truevalue 不是 Noneto_replace 是字符串时,替换将应用于 DataFrame 的所有列。

>>> df = pd.DataFrame({'A': [0, 1, 2, 3, 4],
...                    'B': ['a', 'b', 'c', 'd', 'e'],
...                    'C': ['f', 'g', 'h', 'i', 'j']})
>>> df.replace(to_replace='^[a-g]', value='e', regex=True)
    A  B  C
0  0  e  e
1  1  e  e
2  2  e  h
3  3  e  i
4  4  e  j

如果 value 不是 Noneto_replace 是字典,则字典的键将是应用替换的 DataFrame 列。

>>> df.replace(to_replace={'B': '^[a-c]', 'C': '^[h-j]'}, value='e', regex=True)
    A  B  C
0  0  e  f
1  1  e  g
2  2  e  e
3  3  d  e
4  4  e  e