C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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:DataFrame.replace(to_replace=None, value=None, inplace=False, limit=None, regex=False, method='pad', axis=None) 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:import pandas as pd info = pd.DataFrame({'Language known': ['Python', 'Android', 'C', 'Android', 'Python', 'C++', 'C']}, index=['Parker', 'Smith', 'John', 'William', 'Dean', 'Christina', 'Cornelia']) print(info) dictionary = {"Python": 1, "Android": 2, "C": 3, "Android": 4, "C++": 5} info1 = info.replace({"Language known": dictionary}) print("\n\n") print(info1) 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. import pandas as pd info = pd.DataFrame({ 'name':['Parker','Smith','John'], 'age':[27,34,31], 'city':['US','Belgium','London'] }) info.replace([29],38) 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: import pandas as pd info = pd.DataFrame({ 'name':['Parker','Smith','John'], 'age':[27,34,31], 'city':['US','Belgium','London'] }) info.replace({ 34:29, 'Smith':'William' }) 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: import pandas as pd info = pd.DataFrame({ 'name':['Parker','Smith','John'], 'age':[27,34,31], 'city':['US','Belgium','London'] }) info.replace('Sm.+','Ela',regex=True) Output name age City 0 Parker 27 US 1 Ela 34 Belgium 2 John 31 London
Next TopicDataFrame.iloc[]
|