pandas.Index.replace#

Index.replace(to_replace=None, value=<no_default>, regex=False)[source]#

Replace values in the Index.

Replace values given in to_replace with value. Values of the Index that are not in to_replace are left unchanged.

Parameters:
to_replacestr, regex, list, dict, Series, scalar, or None

The value(s) to be replaced. If a dict is provided, value must be omitted.

valuescalar, dict, list, str, regex, default None

The value to replace occurrences of to_replace with.

regexbool or same types as to_replace, default False

Whether to interpret to_replace and/or value as regular expressions.

Returns:
Index

Index with replaced values.

See also

Series.replace

Replace values in a Series.

DataFrame.replace

Replace values in a DataFrame.

Examples

>>> idx = pd.Index([1, 2, 3, 4, 5])
>>> idx.replace(3, 30)
Index([1, 2, 30, 4, 5], dtype='int64')