Replace Characters in Strings in Pandas DataFrame

Replace Characters in Strings in Pandas DataFrame

Replacing characters in strings within a pandas DataFrame can be achieved using the str.replace() method. This method works on string values and is available for Series objects in pandas.

Here's how to use str.replace():

Example:

Let's say you have a DataFrame with a column 'Name' and you want to replace all occurrences of "John" with "Jon":

import pandas as pd # Sample DataFrame df = pd.DataFrame({ 'Name': ['John Doe', 'Johnathan Smith', 'Johnny Bravo'], 'Age': [25, 30, 22] }) # Replace 'John' with 'Jon' df['Name'] = df['Name'].str.replace('John', 'Jon') print(df) 

This will output:

 Name Age 0 Jon Doe 25 1 Jonathan Smith 30 2 Jonny Bravo 22 

You can see that "John" has been replaced with "Jon" in the 'Name' column.

Replacing Multiple Strings:

If you need to replace multiple different strings, you can chain str.replace() calls:

df['Name'] = df['Name'].str.replace('John', 'Jon').str.replace('Doe', 'D.') 

Or, for a more efficient way, you can use a loop:

replacements = { 'John': 'Jon', 'Doe': 'D.' } for old, new in replacements.items(): df['Name'] = df['Name'].str.replace(old, new) 

Note: By default, str.replace() treats the pattern to replace as a regular expression. If you don't want this behavior and you want to replace exact strings, pass regex=False as an argument:

df['Name'] = df['Name'].str.replace('John', 'Jon', regex=False) 

More Tags

http-status-code-406 spring-boot-configuration fullcalendar-4 backbone.js binary-tree in-app-billing augmented-reality gui-testing fragmentmanager android-location

More Programming Guides

Other Guides

More Programming Examples