Python | Pandas dataframe.replace() - GeeksforGeeks

19 Feb, 2021
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
Pandas dataframe.replace() function is used to replace a string, regex, list, dictionary, series, number etc. from a dataframe. This is a very rich function as it has many variations.The most powerful thing about this function is that it can work with Python regex (regular expressions).
Syntax: DataFrame.replace(to_replace=None, value=None, inplace=False, limit=None, regex=False, method=’pad’, axis=None)
Parameters:to_replace : [str, regex, list, dict, Series, numeric, or None] pattern that we are trying to replace in dataframe.value : 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 : If True, in place. Note: this will modify any other views on this object (e.g. a column from a DataFrame). Returns the caller if this is True.limit : Maximum size gap to forward or backward fillregex : Whether to interpret to_replace and/or value as regular expressions. If this is True then to_replace must be a string. Otherwise, to_replace must be None because this parameter will be interpreted as a regular expression or a list, dict, or array of regular expressions.method : Method to use when for replacement, when to_replace is a list.
Returns: filled : NDFrame
For link to CSV file Used in Code, click here
Example #1: Replace team “Boston Celtics” with “Omega Warrior” in the nba.csv file
# importing pandas as pdimport pandas as pd  # Making data frame from the csv filedf = pd.read_csv("nba.csv")  # Printing the first 10 rows of the data frame for visualizationdf[:10]
Output:
We are going to replace team “Boston Celtics” with “Omega Warrior” in the ‘df’ data frame
# this will replace "Boston Celtics" with "Omega Warrior"df.replace(to_replace ="Boston Celtics",                 value ="Omega Warrior")
Output:
 
Example #2: Replacing more than one value at a time. Using python list as an argument
We are going to replace team “Boston Celtics” and “Texas” with “Omega Warrior” in the ‘df’ dataframe.
# importing pandas as pdimport pandas as pd  # Making data frame from the csv filedf = pd.read_csv("nba.csv")  # this will replace "Boston Celtics" and "Texas" with "Omega Warrior"df.replace(to_replace =["Boston Celtics", "Texas"],                             value ="Omega Warrior")
Output:Notice the College column in the first row, “Texas” has been replaced with “Omega Warriors” Example #3: Replace the Nan value in the data frame with -99999 value.
# importing pandas as pdimport pandas as pd  # Making data frame from the csv filedf = pd.read_csv("nba.csv")  # will replace  Nan value in dataframe with value -99999 df.replace(to_replace = np.nan, value =-99999)
Output:Notice all the Nan value in the data frame has been replaced by -99999. Though for practical purposes we should be careful with what value we are replacing nan value.YouTubeGeeksforGeeks501K subscribersPython | Pandas dataframe.replace() | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.More videosMore videosYou're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 3:09•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=Z2dy-U9rKxk" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
Python pandas-dataFrame
Python pandas-dataFrame-methods
Python-pandas
Technical Scripter 2018
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Python Dictionary
Enumerate() in Python
How to Install PIP on Windows ?
Different ways to create Pandas Dataframe
Reading and Writing to text files in Python
Create a Pandas DataFrame from Lists
sum() function in Python
*args and **kwargs in Python
How to drop one or multiple columns in Pandas Dataframe
Convert integer to string in Python