Python | Pandas dataframe.replace()
Last Updated :
01 Dec, 2023
Pandas dataframe.replace() function is used to replace a string, regex, list, dictionary, series, number, etc. from a Pandas Dataframe in Python. Every instance of the provided value is replaced after a thorough search of the full DataFrame.
Pandas dataframe.replace() Method Syntax
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 fillÂ
- regex : 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
Simple Example of Pandas dataframe.replace()
Here, we are replacing 49.50 with 60.
Python3
import pandas as pd
df = {
"Array_1" : [ 49.50 , 70 ],
"Array_2" : [ 65.1 , 49.50 ]
}
data = pd.DataFrame(df)
print (data.replace( 49.50 , 60 ))
|
Output:
Array_1 Array_2
0 60.0 65.1
1 70.0 60.0
Replace Values in Pandas Dataframe Examples
Here, we are going to see the implementation of dataframe.replace() methods with the help of some examples. For a link to the CSV file Used in Code, click hereÂ
Python3
import pandas as pd
df = pd.read_csv( "nba.csv" )
df[: 10 ]
|
Output:Â
Example 1:Â Replacing a Single Value
We are going to replace team “Boston Celtics” with “Omega Warrior” in the ‘df’ Dataframe.
Python3
df.replace(to_replace = "Boston Celtics" ,
value = "Omega Warrior" )
|
Output:Â
Example 2: Replacing Two Values with a Single Value
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.Â
Python3
import pandas as pd
df = pd.read_csv( "nba.csv" )
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:Â Replacing Nan With a Random Integer Value
Replace the Nan value in the data frame with the -99999 value.Â
Python3
import pandas as pd
df = pd.read_csv( "nba.csv" )
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.
Example 4: Replacing With Multiple Values
In this example, we are replacing multiple values in a Pandas Dataframe by using dataframe.replace() function.
Python3
import pandas as pd
df = pd.read_csv( "nba.csv" )
df1 = df.replace([ 'Boston Celtics' , 'Amir Johnson' , 'R.J. Hunter' ],
[ 'Omega Warriors' , 'Mitcell Johnson' , 'Shivang Thomas' ])
df1[: 10 ]
|
Output
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...