当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python dask.dataframe.Series.replace用法及代码示例


用法:

Series.replace(to_replace=None, value=None, regex=False)

to_replace 中给出的值替换为 value

此文档字符串是从 pandas.core.frame.DataFrame.replace 复制而来的。

可能存在与 Dask 版本的一些不一致之处。

DataFrame 的值被动态替换为其他值。

这与使用 .loc.iloc 更新不同,后者要求您指定要使用某个值更新的位置。

参数

to_replacestr、regex、list、dict、Series、int、float 或 None

如何找到将被替换的值。

  • 数字、str 或正则表达式:

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

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

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

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

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

    • 其次,如果regex=True然后所有的字符串两个都列表将被解释为正则表达式,否则它们将直接匹配。这无关紧要value因为只有几个可能的替换正则表达式可以使用。

    • str、regex 和 numeric 规则如上适用。

  • 字典:

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

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

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

  • None:

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

有关每个示例,请参见示例部分。

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

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

inplacebool,默认 False(在 Dask 中不支持)

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

limitint,默认无(在 Dask 中不支持)

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

regexbool 或与 to_replace 相同的类型,默认为 False

是否翻译to_replace和/或value作为正则表达式。如果这是True然后to_replace 必须成为一个字符串。或者,这可以是一个正则表达式或一个列表、字典或正则表达式数组,在这种情况下to_replace一定是None.

method{‘pad’, ‘ffill’, ‘bfill’, None }(Dask 不支持)

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

返回

DataFrame

替换后的对象。

抛出

AssertionError
  • 如果 regex 不是 bool 并且 to_replace 不是 None
TypeError
  • 如果 to_replace 不是标量,则 array-like、dictNone
  • 如果 to_replacedict 并且 value 不是 list , dict , ndarraySeries
  • 如果 to_replaceNone 并且 regex 不能编译为正则表达式或者是列表、字典、ndarray 或系列。
  • 当替换多个 booldatetime64 对象并且 to_replace 的参数与被替换值的类型不匹配时
ValueError
  • 如果将 listndarray 传递给 to_replacevalue 但它们的长度不同。

注意

  • 使用 re.sub 在后台执行正则表达式替换。 re.sub 的替换规则相同。
  • 正则表达式只会替换字符串,这意味着您不能提供例如匹配浮点数的正则表达式并期望框架中具有数字 dtype 的列被匹配。但是,如果那些浮点数字符串,那么你可以这样做。
  • 这种方法有很多的选项。我们鼓励您尝试和使用这种方法,以直观地了解它是如何工作的。
  • 当dict用作to_replace值时,就好像dict中的key(s)是to_replace部分,dict中的value(s)是value参数。

例子

标量`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

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

正则表达式`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 值时,就像字典中的值等于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’)进行替换。所以这就是为什么在这种情况下,‘a’ 值在第 1 行和第 2 行中被替换为 10,在第 4 行中被替换为 ‘b’。

>>> 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

相关用法


注:本文由纯净天空筛选整理自dask.org大神的英文原创作品 dask.dataframe.Series.replace。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。