cudf.DataFrame.replace#

DataFrame.replace(to_replace=None, value=_NoDefault.no_default, inplace=False, limit=None, regex=False, method=_NoDefault.no_default)#

Replace values given in to_replace with value.

Parameters:
to_replacenumeric, str or list-like

Value(s) to replace.

  • numeric or str:
    • values equal to to_replace will be replaced with value

  • list of numeric or str:
    • If value is also list-like, to_replace and value must be of same length.

  • dict:
    • Dicts can be used to specify different replacement values for different existing values. For example, {‘a’: ‘b’, ‘y’: ‘z’} replaces the value ‘a’ with ‘b’ and ‘y’ with ‘z’. To use a dict in this way the value parameter should be None.

valuescalar, dict, list-like, str, default None

Value to replace any values matching to_replace with.

inplacebool, default False

If True, in place.

Returns:
resultSeries

Series after replacement. The mask and index are preserved.

Raises:
TypeError
  • If to_replace is not a scalar, array-like, dict, or None

  • If to_replace is a dict and value is not a list, dict, or Series

ValueError
  • If a list is passed to to_replace and value but they are not the same length.

See also

Series.fillna

Examples

Series

Scalar to_replace and value

>>> import cudf
>>> s = cudf.Series([0, 1, 2, 3, 4])
>>> s
0    0
1    1
2    2
3    3
4    4
dtype: int64
>>> s.replace(0, 5)
0    5
1    1
2    2
3    3
4    4
dtype: int64

List-like to_replace

>>> s.replace([1, 2], 10)
0     0
1    10
2    10
3     3
4     4
dtype: int64

dict-like to_replace

>>> s.replace({1:5, 3:50})
0     0
1     5
2     2
3    50
4     4
dtype: int64
>>> s = cudf.Series(['b', 'a', 'a', 'b', 'a'])
>>> s
0     b
1     a
2     a
3     b
4     a
dtype: object
>>> s.replace({'a': None})
0       b
1    <NA>
2    <NA>
3       b
4    <NA>
dtype: object

If there is a mismatch in types of the values in to_replace & value with the actual series, then cudf exhibits different behavior with respect to pandas and the pairs are ignored silently:

>>> s = cudf.Series(['b', 'a', 'a', 'b', 'a'])
>>> s
0    b
1    a
2    a
3    b
4    a
dtype: object
>>> s.replace('a', 1)
0    b
1    a
2    a
3    b
4    a
dtype: object
>>> s.replace(['a', 'c'], [1, 2])
0    b
1    a
2    a
3    b
4    a
dtype: object

DataFrame

Scalar to_replace and value

>>> import cudf
>>> df = cudf.DataFrame({'A': [0, 1, 2, 3, 4],
...                    'B': [5, 6, 7, 8, 9],
...                    'C': ['a', 'b', 'c', 'd', 'e']})
>>> df
   A  B  C
0  0  5  a
1  1  6  b
2  2  7  c
3  3  8  d
4  4  9  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

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

Pandas Compatibility Note

DataFrame.replace, Series.replace

Parameters that are currently not supported are: limit, regex, method