Pandas DataFrame.replace()Pandas replace() is a very rich function that is used to replace a string, regex, dictionary, list, and series from the DataFrame. The values of the DataFrame can be replaced with other values dynamically. It is capable of working with the Python regex(regular expression). It differs from updating with .loc or .iloc, which requires you to specify a location where you want to update with some value. Syntax:Parameters:
Note: It will also modify any other views on this object (e.g., a column from a DataFrame). Returns the caller if this is True.
Returns: It returns a DataFrame object after the replacement. Example1:Output Language known Parker Python Smith Android John C William Android Dean Python Christina C++ Cornelia C Language known Parker 1 Smith 4 John 3 William 4 Dean 1 Christina 5 Cornelia 3 Example2:The below example replaces a value with another in a DataFrame. Output name age City 0 Parker 27 US 1 Smith 34 Belgium 2 John 38 London Example3:The below example replaces the values from a dict: Output name age City 0 Parker 27 US 1 William 29 Belgium 2 John 31 London Example4:The below example replaces the values from regex: Output name age City 0 Parker 27 US 1 Ela 34 Belgium 2 John 31 London Next TopicDataFrame.iloc[] |