cudf.core.column.string.StringMethods.replace_tokens#

StringMethods.replace_tokens(targets, replacements, delimiter: str | None = None) SeriesOrIndex[source]#

The targets tokens are searched for within each string in the series and replaced with the corresponding replacements if found. Tokens are identified by the delimiter character provided.

Parameters:
targetsarray-like, Sequence or Series

The tokens to search for inside each string.

replacementsarray-like, Sequence, Series or str

The strings to replace for each found target token found. Alternately, this can be a single str instance and would be used as replacement for each string found.

delimiterstr

The character used to locate the tokens of each string. Default is whitespace.

Returns:
Series or Index of object.

Examples

>>> import cudf
>>> sr = cudf.Series(["this is me", "theme music", ""])
>>> targets = cudf.Series(["is", "me"])
>>> sr.str.replace_tokens(targets=targets, replacements="_")
0       this _ _
1    theme music
2
dtype: object
>>> sr = cudf.Series(["this;is;me", "theme;music", ""])
>>> sr.str.replace_tokens(targets=targets, replacements=":")
0     this;is;me
1    theme;music
2
dtype: object