- API reference
- Series
- pandas.Serie...
pandas.Series.str.replace#
- Series.str.replace(pat,repl,n=-1,case=None,flags=0,regex=False)[source]#
Replace each occurrence of pattern/regex in the Series/Index.
Equivalent to
str.replace()orre.sub(), depending onthe regex value.- Parameters:
- patstr or compiled regex
String can be a character sequence or regular expression.
- replstr or callable
Replacement string or a callable. The callable is passed the regexmatch object and must return a replacement string to be used.See
re.sub().- nint, default -1 (all)
Number of replacements to make from start.
- casebool, default None
Determines if replace is case sensitive:
If True, case sensitive (the default ifpat is a string)
Set to False for case insensitive
Cannot be set ifpat is a compiled regex.
- flagsint, default 0 (no flags)
Regex module flags, e.g. re.IGNORECASE. Cannot be set ifpat is a compiledregex.
- regexbool, default False
Determines if the passed-in pattern is a regular expression:
If True, assumes the passed-in pattern is a regular expression.
If False, treats the pattern as a literal string
Cannot be set to False ifpat is a compiled regex orrepl isa callable.
- Returns:
- Series or Index of object
A copy of the object with all matching occurrences ofpat replaced byrepl.
- Raises:
- ValueError
ifregex is False andrepl is a callable orpat is a compiledregex
ifpat is a compiled regex andcase orflags is set
Notes
Whenpat is a compiled regex, all flags should be included in thecompiled regex. Use ofcase,flags, orregex=False with a compiledregex will raise an error.
Examples
Whenpat is a string andregex is True, the givenpatis compiled as a regex. Whenrepl is a string, it replaces matchingregex patterns as with
re.sub(). NaN value(s) in the Series areleft as is:>>>pd.Series(['foo','fuz',np.nan]).str.replace('f.','ba',regex=True)0 bao1 baz2 NaNdtype: object
Whenpat is a string andregex is False, everypat is replaced withrepl as with
str.replace():>>>pd.Series(['f.o','fuz',np.nan]).str.replace('f.','ba',regex=False)0 bao1 fuz2 NaNdtype: object
Whenrepl is a callable, it is called on everypat using
re.sub(). The callable should expect one positional argument(a regex object) and return a string.To get the idea:
>>>pd.Series(['foo','fuz',np.nan]).str.replace('f',repr,regex=True)0 <re.Match object; span=(0, 1), match='f'>oo1 <re.Match object; span=(0, 1), match='f'>uz2 NaNdtype: object
Reverse every lowercase alphabetic word:
>>>repl=lambdam:m.group(0)[::-1]>>>ser=pd.Series(['foo 123','bar baz',np.nan])>>>ser.str.replace(r'[a-z]+',repl,regex=True)0 oof 1231 rab zab2 NaNdtype: object
Using regex groups (extract second group and swap case):
>>>pat=r"(?P<one>\w+) (?P<two>\w+) (?P<three>\w+)">>>repl=lambdam:m.group('two').swapcase()>>>ser=pd.Series(['One Two Three','Foo Bar Baz'])>>>ser.str.replace(pat,repl,regex=True)0 tWO1 bARdtype: object
Using a compiled regex with flags
>>>importre>>>regex_pat=re.compile(r'FUZ',flags=re.IGNORECASE)>>>pd.Series(['foo','fuz',np.nan]).str.replace(regex_pat,'bar',regex=True)0 foo1 bar2 NaNdtype: object