IconResources
Conduct analysis

Find and replace

Locate and replace values in a column, such as standardizing names or correcting entries.

Example Data

Follow along with right out of the box example data. Copy following data in the information request of the agent you are working in.



Sometimes your data isn't consistent: a country might be written as "USA" in one row and "United States" in another, or a product type might be misspelled. Just like in Excel, finding and replacing values helps standardize your data so it’s easier to group, count, or analyze.

In this section, you’ll learn how to search for values in a column and replace them with something else — either one by one or as a group.

Replace one value with another

Excel

In Excel, you use "Find and Replace" (Ctrl+H) to change all instances of a word or number in a column.

t0 Prompt

Replace "USA" with "United States"

Change "Services" to "Management Services"

Find and fix a value in column X from "Z" to "Y"

Code

The python code looks as follows:

transactions["Buyer Country"] = transactions["Buyer Country"].replace("USA", "United States")
transactions

Replace multiple values

Excel

In Excel, you would run several “Find and Replace” actions — one after the other — to fix multiple values.

t0 Prompt

Change "UK" to "United Kingdom" and "USA" to "United States"

Standardize the names in column X

Code

The python code looks as follows:

transactions["Seller Country"] = transactions["Seller Country"].replace({
    "UK": "United Kingdom",
    "USA": "United States"
})
transactions

Replace partial text using string functions

Excel

In Excel, you might use SUBSTITUTE() or REPLACE() formulas to swap out part of a string in a cell.

t0 Prompt

Replace "Solara Dynamics" with "SD" in company names

Simplify all names in the Seller column

Shorten long names to initials

Code

The python code looks as follows:

transactions["Seller"] = transactions["Seller"].str.replace("Solara Dynamics", "SD", regex=False)
transactions
FunctionDescription
replace("old", "new")Replaces exact value
replace({ ... })Replaces multiple values at once
str.replace("old", "new")Replaces part of text within strings

On this page