pandas.Series.replace
-
Series.replace(to_replace=None, value=None, inplace=False, limit=None, regex=False, method='pad')[source] -
Replace values given in
to_replacewithvalue.Values of the Series 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.Parameters: -
to_replace : str, regex, list, dict, Series, int, float, or None -
How to find the values that will be replaced.
-
numeric, str or regex:
- numeric: numeric values equal to
to_replacewill be replaced withvalue - str: string exactly matching
to_replacewill be replaced withvalue - regex: regexs matching
to_replacewill be replaced withvalue
- numeric: numeric values equal to
-
list of str, regex, or numeric:
- First, if
to_replaceandvalueare 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 forvaluesince there are only a few possible substitution regexes you can use. - str, regex and numeric rules apply as above.
- First, if
-
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 thevalueparameter should beNone. - For a DataFrame a dict can specify that different values should be replaced in different columns. For example,
{'a': 1, 'b': 'z'}looks for the value 1 in column ‘a’ and the value ‘z’ in column ‘b’ and replaces these values with whatever is specified invalue. Thevalueparameter should not beNonein this case. You can treat this as a special case of passing two lists except that you are specifying the column to search in. - For a DataFrame nested dictionaries, e.g.,
{'a': {'b': np.nan}}, are read as follows: look in column ‘a’ for the value ‘b’ and replace it with NaN. Thevalueparameter should beNoneto use a nested dict in this way. You can nest regular expressions as well. Note that column names (the top-level dictionary keys in a nested dictionary) cannot be regular expressions.
- Dicts can be used to specify different replacement values for different existing values. For example,
-
None:
- This means that the
regexargument must be a string, compiled regular expression, or list, dict, ndarray or Series of such elements. Ifvalueis alsoNonethen this must be a nested dictionary or Series.
- This means that the
See the examples section for examples of each of these.
-
-
value : scalar, dict, list, str, regex, default None -
Value to replace any values matching
to_replacewith. 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. -
inplace : bool, default False -
If True, in place. Note: this will modify any other views on this object (e.g. a column from a DataFrame). Returns the caller if this is True.
-
limit : int, default None -
Maximum size gap to forward or backward fill.
-
regex : bool or same types as to_replace, default False -
Whether to interpret
to_replaceand/orvalueas regular expressions. If this isTruethento_replacemust be a string. Alternatively, this could be a regular expression or a list, dict, or array of regular expressions in which caseto_replacemust beNone. -
method : {‘pad’, ‘ffill’, ‘bfill’, None} -
The method to use when for replacement, when
to_replaceis a scalar, list or tuple andvalueisNone.Changed in version 0.23.0: Added to DataFrame.
Returns: - Series
-
Object after replacement.
Raises: - AssertionError
-
- If
regexis not aboolandto_replaceis notNone.
- If
- TypeError
-
- If
to_replaceis adictandvalueis not alist,dict,ndarray, orSeries - If
to_replaceisNoneandregexis not compilable into a regular expression or is a list, dict, ndarray, or Series. - When replacing multiple
boolordatetime64objects and the arguments toto_replacedoes not match the type of the value being replaced
- If
- ValueError
-
- If a
listor anndarrayis passed toto_replaceandvaluebut they are not the same length.
- If a
See also
-
Series.fillna - Fill NA values.
-
Series.where - Replace values based on boolean condition.
-
Series.str.replace - Simple string replacement.
Notes
- Regex substitution is performed under the hood with
re.sub. The rules for substitution forre.subare the same. - Regular expressions will only substitute on strings, meaning you cannot provide, for example, a regular expression matching floating point numbers and expect the columns in your frame that have a numeric dtype to be matched. However, if those floating point numbers are strings, then you can do this.
- This method has a lot of options. You are encouraged to experiment and play with this method to gain intuition about how it works.
- When dict is used as the
to_replacevalue, it is like key(s) in the dict are the to_replace part and value(s) in the dict are the value parameter.
Examples
Scalar `to_replace` and `value`
>>> s = pd.Series([0, 1, 2, 3, 4]) >>> s.replace(0, 5) 0 5 1 1 2 2 3 3 4 4 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 eList-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 0 1 3 2 3 3 3 4 4 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 eRegular expression `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 xyzNote that when replacing multiple
boolordatetime64objects, the data types in theto_replaceparameter must match the data type of the value being replaced:>>> df = pd.DataFrame({'A': [True, False, True], ... 'B': [False, True, False]}) >>> df.replace({'a string': 'new value', True: False}) # raises Traceback (most recent call last): ... TypeError: Cannot compare types 'ndarray(dtype=bool)' and 'str'This raises a
TypeErrorbecause one of thedictkeys is not of the correct type for replacement.Compare the behavior of
s.replace({'a': None})ands.replace('a', None)to understand the peculiarities of theto_replaceparameter:>>> s = pd.Series([10, 'a', 'a', 'b', 'a'])
When one uses a dict as the
to_replacevalue, it is like the value(s) in the dict are equal to thevalueparameter.s.replace({'a': None})is equivalent tos.replace(to_replace={'a': None}, value=None, method=None):>>> s.replace({'a': None}) 0 10 1 None 2 None 3 b 4 None dtype: objectWhen
value=Noneandto_replaceis a scalar, list or tuple,replaceuses the method parameter (default ‘pad’) to do the replacement. So this is why the ‘a’ values are being replaced by 10 in rows 1 and 2 and ‘b’ in row 4 in this case. The commands.replace('a', None)is actually equivalent tos.replace(to_replace='a', value=None, method='pad'):>>> s.replace('a', None) 0 10 1 10 2 10 3 b 4 b dtype: object -
© 2008–2012, AQR Capital Management, LLC, Lambda Foundry, Inc. and PyData Development Team
Licensed under the 3-clause BSD License.
https://pandas.pydata.org/pandas-docs/version/0.24.2/reference/api/pandas.Series.replace.html