pandas.Series.replace
-
Series.replace(to_replace=None, value=None, inplace=False, limit=None, regex=False, method='pad', axis=None) -
Replace values given in ‘to_replace’ with ‘value’.
Parameters: to_replace : str, regex, list, dict, Series, numeric, or None
-
str or regex:
- str: string exactly matching
to_replacewill be replaced withvalue - regex: regexs matching
to_replacewill be replaced withvalue
- str: string exactly matching
-
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 and regex rules apply as above.
- First, if
-
dict:
- Nested dictionaries, e.g., {‘a’: {‘b’: nan}}, are read as follows: look in column ‘a’ for the value ‘b’ and replace it with nan. You can nest regular expressions as well. Note that column names (the top-level dictionary keys in a nested dictionary) cannot be regular expressions.
- Keys map to column names and values map to substitution values. You can treat this as a special case of passing two lists except that you are specifying the column to search in.
-
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 orSeries.
- This means that the
See the examples section for examples of each of these.
value : scalar, dict, list, str, regex, default None
Value to use to fill holes (e.g. 0), alternately a dict of values specifying 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 : boolean, default False
If True, in place. Note: this will modify any other views on this object (e.g. a column form 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 FalseWhether to interpret
to_replaceand/orvalueas regular expressions. If this isTruethento_replacemust be a string. Otherwise,to_replacemust beNonebecause this parameter will be interpreted as a regular expression or a list, dict, or array of regular expressions.method : string, optional, {‘pad’, ‘ffill’, ‘bfill’}
The method to use when for replacement, when
to_replaceis alist.Returns: filled : NDFrame
Raises: AssertionError
- If
regexis not aboolandto_replaceis notNone.
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.
ValueError
- If
to_replaceandvaluearelists orndarrays, but they are not the same length.
See also
NDFrame.reindex,NDFrame.asfreq,NDFrame.fillnaNotes
- 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.
-
© 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.18.1/generated/pandas.Series.replace.html