Pandas Replace
In this Python Pandas tutorial we’re going to be taking a look at replace() which allows you to match a full value within your data frame and then replace it with something that you want.Ā
This article will cover seven different examples of Pandas replace increasing the complexity along the way.
We are going to start out by importing in two libraries for this tutorial: Pandas and NumPy.
import pandas as pd import numpy as np #Similiar to map # .replace() is not operating on the contents of the DataFrame as stringsāit's trying to match the entire value
Next, we will create a dataframe to use for the article. This will be done by passing in a dictionary to pd.DataFrame(). The dictionary we are using is of different baseball players with their homerun totals and teams that they played for.
data = { 'Player': ['Barry Bonds', 'Hank Aaron', 'Babe Ruth', 'Alex Rodriguez', 'Albert Pujols', 'Willie Mays', 'Ken Griffey Jr.'], 'HR': [762, 755, 714, 696, 703, 660, np.nan], #630 replace by nnan 'Team': ['Giants', 'Braves', 'Yankes', 'Yanks', 'Cardinals', 'New York Giants', 'Mariners'] }
The code below created the dataframe df.
df = pd.DataFrame(data)

#DataFrame.replace(to_replace=None, value=None, inplace=False, limit=None, regex=False, method=None)
Example 1 Single Value
In example number one we are going to replace a single value.
We will be changing Braves to Atlanta Braves. To do this we select the column we want to replace within. In the first parameter pass in Braves in single quotes. Use a comma and then pass in ‘Atlanta Braves’ which will be replacing Braves.
df['Team'] = df['Team'].replace('Braves', 'Atlanta Braves')
The new dataframe is down below where you can see Braves has been replaced.

Example 2 Multiple Values, one value
df['Team'] = df['Team'].replace(['Yankes', 'Yanks'], 'New York Yankees')
df

Example 3 Multiple Values, multiple values - Dict
df['Team'] = df['Team'].replace({'Giants': 'San Francisco Giants', 'Mariners': 'Seattle Mariners'})
df

Example 4 Multiple Values, multiple values - list
df['Team'] = df['Team'].replace(['San Francisco Giants', 'Atlanta Braves'], ['SF Giants', 'ATL Braves'])
df

Example 5 replace entire dataframe, regardless of column or row
data2 = { 'team_one_city': ['Los Angeles'], 'team_two_city': ['Los Angeles'] }
df2 = pd.DataFrame(data2)
df2

df2.replace('Los Angeles', 'LA')

Example 6 Replace using regex (Partial Strings)
data3 = { 'team_one': ['Los Angeles Angeles'], 'team_two': ['Los Angeles Dodgers'] }
df3 = pd.DataFrame(data3)
df3

df3.replace('Los Angeles', 'LA') #This does Nothing!
df3.replace('Los Angeles', 'LA', regex=True)

Example 7 Replace with $ Example
data4 = { 'Card Name': ['Ken Griffey Jr.', 'Derek Jeter', 'Mike Trout', 'Babe Ruth'], 'Sold Price': ['$1,200.00', '$3,500.50', '$2,000.00', '$10,000.00'] }
sales_df = pd.DataFrame(data4)
sales_df['Sold Price'] = sales_df['Sold Price'].replace( '[\$,)]','', regex=True )
sales_df

Ryan is a Data Scientist at a fintech company, where he focuses on fraud prevention in underwriting and risk. Before that, he worked as a Data Analyst at a tax software company. He holds a degree in Electrical Engineering from UCF.