search
Search
Publish
menu
menu search toc more_vert
Robocat
Guest 0reps
Thanks for the thanks!
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
help Ask a question
Share on Twitter
search
keyboard_voice
close
Searching Tips
Search for a recipe: "Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to
A
A
share
thumb_up_alt
bookmark
arrow_backShare
Twitter
Facebook

Pandas Series str | replace method

Programming
chevron_right
Python
chevron_right
Pandas
chevron_right
Documentation
chevron_right
Series
chevron_right
String Operations
schedule Jul 1, 2022
Last updated
local_offer PythonPandas
Tags

Pandas Series str.replace(~) method replaces a substring of each string in the Series with a specified value. This operation is not done inplace, meaning a new Series is returned and the original is kept intact.

Parameters

1. pat | string or regex

The substring to replace. Since regex=True by default, pat will be treated as a regular expression.

2. repl | string or callable

The value to replace pat. If a callable is passed, then it takes as argument the matched regex pattern and returns a string that replaces the match.

3. n | int | optional

The maximum number (inclusive) of replacements to make. By default, there is no limit as to how many replacements are made.

4. case | boolean or None | optional

Whether or not the matching is to be case-sensitive:

Value

Description

True

Matching is case sensitive.

False

Matching is not case sensitive.

None

The case when regex is used.

By default, case=None (since regex=True by default). Note that case cannot be set if pat is a compiled regex.

5. flags | int | optional

The flags found in the standard regex module (e.g. re.IGNORECASE). By default, flags=0. Note that flags cannot be set if pat is a compiled regular expression.

6. regex | boolean | optional

Whether or not pat is to be treated as a regular expression. By default, regex=True.

Return Value

A new Series with the replaced substrings.

Examples

Basic usage

To replace the substring "A" with "c" for each string in the Series:

s = pd.Series(["aA", "bA", "Ac"])
s.str.replace("A", "c")
0 ac
1 bc
2 cc
dtype: object

Using regex

By default, regex=True, which means that you can directly pass in a regular expression like so:

s = pd.Series(["aA", "bA", "Ac"])
s.str.replace(".*A$", "c")   # regex=True
0 c
1 c
2 Ac
dtype: object

Here, we are replacing substrings that end with A.

Using compiled regex

To use a compiled regex instead of a regex in string form:

import re
s = pd.Series(["aA", "bA", "Ac"])
my_regex = re.compile("a", flags=re.IGNORECASE)
s.str.replace(my_regex, "D")
0 DD
1 bD
2 Dc
dtype: object

Passing in a callable for repl

The function that you pass to repl is given one argument - the regex object that captures the match. This function must return a string that will replace the match.

For instance, consider the following:

s = pd.Series(["aA", "bA", "Ac"])

def foo(my_regex):
match = my_regex.group(0)
return match.upper()

s.str.replace(".*A$", foo)
0 AA
1 BA
2 Ac
dtype: object

Here, we are replacing every string that ends with A by its uppercase form. Just to clarify, foo is called twice in this case and if we were to print the value of match, we would see the following:

aA
bA
robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!