bigframes.geopandas.GeoSeries.replace#
- GeoSeries.replace(to_replace: Any, value: Any = None, *, regex: bool = False)#
Replace values given in to_replace with value.
Values of the Series/DataFrame are replaced with other values dynamically. This differs from updating with
.locor.iloc, which require you to specify a location to update with some value.Examples:
>>> import bigframes.pandas as bpd >>> s = bpd.Series([1, 2, 3, 4, 5]) >>> s 0 1 1 2 2 3 3 4 4 5 dtype: Int64
>>> s.replace(1, 5) 0 5 1 2 2 3 3 4 4 5 dtype: Int64
You can replace a list of values:
>>> s.replace([1, 3, 5], -1) 0 -1 1 2 2 -1 3 4 4 -1 dtype: Int64
You can use a replacement mapping:
>>> s.replace({1: 5, 3: 10}) 0 5 1 2 2 10 3 4 4 5 dtype: Int64
With a string Series you can use a simple string replacement or a regex replacement:
>>> s = bpd.Series(["Hello", "Another Hello"]) >>> s.replace("Hello", "Hi") 0 Hi 1 Another Hello dtype: string
>>> s.replace("Hello", "Hi", regex=True) 0 Hi 1 Another Hi dtype: string
>>> s.replace("^Hello", "Hi", regex=True) 0 Hi 1 Another Hello dtype: string
>>> s.replace("Hello$", "Hi", regex=True) 0 Hi 1 Another Hi dtype: string
>>> s.replace("[Hh]e", "__", regex=True) 0 __llo 1 Anot__r __llo dtype: string
- Parameters:
to_replace (str, regex, list, int, float or None) –
How to find the values that will be replaced.
numeric, str or regex:
numeric: numeric values equal to to_replace will be replaced with value
str: string exactly matching to_replace will be replaced with value
regex: regexs matching to_replace will be replaced with value
list of str, regex, or numeric:
First, if to_replace and value are both lists, they must be the same length.
Second, if
regex=Truethen all of the strings in both lists will be interpreted as regexs otherwise they will match directly. This doesn’t matter much for value since there are only a few possible substitution regexes you can use.str, regex and numeric rules apply as above.
value (scalar, default None) – Value to replace any values matching to_replace with. For a DataFrame a dict of values can be used to specify which value to use for each column (columns not in the dict will not be filled). Regular expressions, strings and lists or dicts of such objects are also allowed.
regex (bool, default False) – Whether to interpret to_replace and/or value as regular expressions. If this is
Truethen to_replace must be a string.
- Returns:
Object after replacement.
- Return type:
- Raises:
If to_replace is not a scalar, array-like,
dict, orNone* If to_replace is adictand value is not alist,dict,ndarray, orSeries* If to_replace isNoneand regex is not compilable into a regular expression or is a list, dict, ndarray, or Series. * When replacing multipleboolordatetime64objects and the arguments to to_replace does not match the type of the value being replaced