-
Series.replace(to_replace=None, value=None, inplace=False, limit=None, regex=False, method='pad', axis=None)
[source] -
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_replace
will be replaced withvalue
- regex: regexs matching
to_replace
will be replaced withvalue
- str: string exactly matching
-
list of str, regex, or numeric:
- First, if
to_replace
andvalue
are both lists, they must be the same length. - Second, if
regex=True
then all of the strings in both lists will be interpreted as regexs otherwise they will match directly. This doesn?t matter much forvalue
since 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
regex
argument must be a string, compiled regular expression, or list, dict, ndarray or Series of such elements. Ifvalue
is alsoNone
then 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_replace
and/orvalue
as regular expressions. If this isTrue
thento_replace
must be a string. Otherwise,to_replace
must beNone
because 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_replace
is alist
.Returns: filled : NDFrame
Raises: AssertionError
- If
regex
is not abool
andto_replace
is notNone
.
TypeError
- If
to_replace
is adict
andvalue
is not alist
,dict
,ndarray
, orSeries
- If
to_replace
isNone
andregex
is not compilable into a regular expression or is a list, dict, ndarray, or Series.
ValueError
- If
to_replace
andvalue
arelist
s orndarray
s, but they are not the same length.
See also
NDFrame.reindex
,NDFrame.asfreq
,NDFrame.fillna
Notes
- Regex substitution is performed under the hood with
re.sub
. The rules for substitution forre.sub
are 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.
-
Series.replace()
2017-01-12 04:54:35
Please login to continue.