Pandas DataFrame DataFrame.replace() function
pandas.DataFrame.replace() replaces values in a DataFrame with other values, which can be strings, regular expressions, lists, dictionaries, Seriesor numbers.
pandas.DataFrame.replace()grammar
DataFrame.replace(,
to_replace=None,
value=None,
inplace=False,
limit=None,
regex=False,
method='pad')
parameter
to_replace |
string, regular expression, list, dict, Series, number or None. The values in the DataFrame to be replaced. |
value |
Scalar, dict, list, string, regular expression, or None. Replace any to_replacevalue that matches . |
inplace |
Boolean value. If true, Truemodifies the caller'sDataFrame |
limit |
Integer. Maximum gap size to fill forward or backward |
regex |
Boolean. If to_replaceand/or valueis a regular expression, then regexset to True. |
method |
Method for replacement |
Return Value
It returns a that replaces all specified fields DataFramewith the given .value
Example Code: pandas.DataFrame.replace()Replace values in a DataFrame using
import pandas as pd
df = pd.DataFrame({'X': [1, 2, 3,],
'Y': [4, 1, 8]})
print("Before Replacement")
print(df)
replaced_df=df.replace(1, 5)
print("After Replacement")
print(replaced_df)
Output:
Before Replacement
X Y
0 1 4
1 2 1
2 3 8
After Replacement
X Y
0 5 4
1 2 5
2 3 8
Here, 1represents to_replacethe parameter and 5represents the parameter replace()in the method value. Therefore, in df, all 1elements with value of are 5replaced by .
pandas.DataFrame.replace()Example Code: Replace Multiple Values in a DataFrame Using
Using List Replacement
import pandas as pd
df = pd.DataFrame({'X': [1, 2, 3,],
'Y': [4, 1, 8]})
print("Before Replacement")
print(df)
replaced_df=df.replace([1,2,3],[1,4,9])
print("After Replacement")
print(replaced_df)
Output:
Before Replacement
X Y
0 1 4
1 2 1
2 3 8
After Replacement
X Y
0 1 4
1 4 1
2 9 8
Here, [1,2,3]represents to_replacethe parameter and [1,4,9]represents the parameter replace()in the method value. Therefore, in df, the column [1,2,3]is [1,4,9]replaced by .
Use dictionary replacement
import pandas as pd
df = pd.DataFrame({'X': [1, 2, 3,],
'Y': [3, 1, 8]})
print("Before Replacement")
print(df)
replaced_df=df.replace({1:10,3:30})
print("After Replacement")
print(replaced_df)
Output:
Before Replacement
X Y
0 1 3
1 2 1
2 3 8
After Replacement
X Y
0 10 30
1 2 10
2 30 8
1It replaces all elements whose value is with , and replaces 10all elements whose value is with .330
Use RegexReplace
import pandas as pd
df = pd.DataFrame({'X': ["zeppy", "amid", "amily"],
'Y': ["xar", "abc", "among"]})
print("Before Replacement")
print(df)
df.replace(to_replace=r'^ami.$', value='song', regex=True,inplace=True)
print("After Replacement")
print(df)
Output:
Before Replacement
X Y
0 zeppy xar
1 amid abc
2 amily among
After Replacement
X Y
0 zeppy xar
1 song abc
2 amily among
It replaces all elements whose first three characters are amifollowed by any character with song. Here only amidsatisfies the given regular expression, so only is replaced amidby song. Although amilythe first three characters of are ami, amithere are two characters after . Therefore, amilydoes not satisfy the given regex, so it remains the same and is not replaced. If you are using regular expressions, make sure regexthat is set to True, and modify the original after inplace=Truecalling the method .replace()DataFrame
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Pandas DataFrame DataFrame.query() function
Publish Date:2025/04/30 Views:107 Category:Python
-
The pandas.DataFrame.query() method filters the rows of the caller DataFrame using the given query expression. pandas.DataFrame.query() grammar DataFrame . query(expr, inplace = False , ** kwargs) parameter expr Filter rows based on query e
Pandas DataFrame DataFrame.min() function
Publish Date:2025/04/30 Views:162 Category:Python
-
Python Pandas DataFrame.min() function gets the minimum value of the DataFrame object along the specified axis. pandas.DataFrame.min() grammar DataFrame . mean(axis = None , skipna = None , level = None , numeric_only = None , ** kwargs) pa
Pandas DataFrame DataFrame.mean() function
Publish Date:2025/04/30 Views:85 Category:Python
-
Python Pandas DataFrame.mean() function calculates the mean of the values of the DataFrame object over the specified axis. pandas.DataFrame.mean() grammar DataFrame . mean(axis = None , skipna = None , level = None , numeric_only = No
Pandas DataFrame DataFrame.isin() function
Publish Date:2025/04/30 Views:133 Category:Python
-
The pandas.DataFrame.isin(values) function checks whether each element in the caller DataFrame contains values the value specified in the input . pandas.DataFrame.isin(values) grammar DataFrame . isin(values) parameter values iterable - lis
Pandas DataFrame DataFrame.groupby() function
Publish Date:2025/04/30 Views:161 Category:Python
-
pandas.DataFrame.groupby() takes a DataFrame as input and divides the DataFrame into groups based on a given criterion. We can use groupby() the method to easily process large datasets. pandas.DataFrame.groupby() grammar DataFrame . groupby
Pandas DataFrame DataFrame.fillna() function
Publish Date:2025/04/30 Views:60 Category:Python
-
The pandas.DataFrame.fillna() function replaces the values DataFrame in NaN with a certain value. pandas.DataFrame.fillna() grammar DataFrame . fillna( value = None , method = None , axis = None , inplace = False , limit = None , down
Pandas DataFrame DataFrame.dropna() function
Publish Date:2025/04/30 Views:181 Category:Python
-
The pandas.DataFrame.dropna() function removes null values (missing values) from a DataFrame by dropping rows or columns that contain null values DataFrame . NaN ( Not a Number ) and NaT ( Not a Time ) represent null values. DataFrame
Pandas DataFrame DataFrame.assign() function
Publish Date:2025/04/30 Views:55 Category:Python
-
Python Pandas DataFrame.assign() function assigns new columns to DataFrame . pandas.DataFrame.assign() grammar DataFrame . assign( ** kwargs) parameter **kwargs Keyword arguments, DataFrame the column names to be assigned to are passed as k
Pandas DataFrame DataFrame.transform() function
Publish Date:2025/04/30 Views:120 Category:Python
-
Python Pandas DataFrame.transform() DataFrame applies a function on and transforms DataFrame . The function to be applied is passed as an argument to the function. The axis length of transform() the transformed should be the same as the ori

