pandas.Series.replace#

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

替换中给出的值 to_replace 使用 value

系列的值将动态替换为其他值。

这不同于使用 .loc.iloc ,这需要您指定一个位置以使用某个值进行更新。

参数
to_replace字符串、正则表达式、列表、DICT、系列、整型、浮点型或无

如何查找将被替换的值。

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

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

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

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

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

    • 首先,如果 to_replacevalue 都是列表,他们 must 保持相同的长度。

    • 第二,如果 regex=True 然后中的所有字符串 both 列表将被解释为正则表达式,否则它们将直接匹配。这对你来说并不重要 value 因为只有几种可能的替代正则表达式可以使用。

    • 字符串、正则表达式和数字规则如上适用。

  • DICT:

    • DICTS可用于为不同的现有值指定不同的替换值。例如, {{'a': 'b', 'y': 'z'}} 将值‘a’替换为‘b’,将‘y’替换为‘z’。以这种方式使用词典 value 参数应为 None

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

    • 对于DataFrame嵌套词典,例如, {{'a': {{'b': np.nan}}}} ,如下所示:在列‘a’中查找值‘b’,并将其替换为NaN。这个 value 参数应为 None 以这种方式使用嵌套的字典。您也可以嵌套正则表达式。请注意,列名(嵌套词典中的顶级词典键) 不能 为正则表达式。

  • 无:

    • 这意味着 regex 参数必须是字符串、已编译的正则表达式或此类元素的列表、字典、ndarray或系列。如果 value 也是 None 然后这个 must 为嵌套词典或系列。

请参阅示例部分以获取其中每个示例的示例。

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

值来替换任何匹配的值 to_replace 和.。对于DataFrame,可以使用值的字典来指定为每一列使用哪个值(不在字典中的列将不被填充)。也允许此类对象的正则表达式、字符串和列表或词典。

inplace布尔值,默认为False

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

limitInt,默认为无

向前或向后填充的最大尺寸间隙。

正则化 :Bool或与相同类型 to_replace ,默认为FALSEBool或与相同类型

Whether to interpret to_replace and/or value as regular expressions. If this is True then to_replace must be a string. Alternatively, this could be a regular expression or a list, dict, or array of regular expressions in which case to_replace must be None.

方法 :{‘填充’, None }{‘Pad’,‘ffill’,‘bill’,

替换时使用的方法, to_replace 是标量、列表或元组,并且 valueNone

在 0.23.0 版更改: 已添加到DataFrame。

退货
系列

替换后的对象。

加薪
AssertionError
  • 如果 regex 不是一个 boolto_replace 不是 None

TypeError
  • 如果 to_replace 不是标量的、类似数组的 dict ,或 None

  • 如果 to_replace 是一种 dictvalue 不是一个 listdictndarray ,或 Series

  • 如果 to_replaceNoneregex 不能编译为正则表达式,或者是列表、DICT、ndarray或Series。

  • 当更换多个 booldatetime64 对象和参数 to_replace 与要替换的值的类型不匹配

ValueError
  • 如果一个 list 或一个 ndarray 传递给 to_replacevalue 但它们的长度并不相同。

参见

Series.fillna

填充NA值。

Series.where

根据布尔条件替换值。

Series.str.replace

简单的字符串替换。

注意事项

  • 正则表达式替换是在幕后执行的 re.sub 。替换的规则 re.sub 都是一样的。

  • 正则表达式将只替换字符串,这意味着您不能提供匹配浮点数的正则表达式,也不能期望匹配框架中具有数字数据类型的列。然而,如果那些浮点数 are 弦,那么你就能做到这一点。

  • 此方法具有 很多 有很多选择。我们鼓励您尝试并尝试这种方法,以直观地了解它是如何工作的。

  • 当将dict用作 to_replace 值,就像DICT中的键是TO_REPLACE部分,而DICT中的值是VALUE参数。

示例

Scalar `to_replace` and `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

List-like `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

dict-like `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

Regular expression `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'])

当一个人使用一个词典作为 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

什么时候 value 未显式传递,并且 to_replace 是标量、列表或元组, replace 使用方法参数(默认为‘Pad’)进行替换。这就是为什么在本例中,第1行和第2行中的‘a’值被替换为10,而在本例中,第4行中的‘b’值被替换为10。

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

另一方面,如果 None 被显式传递给 value ,它将得到尊重:

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

在 1.4.0 版更改: 以前的显式 None 被默默地忽视了。