Python Replace all occurrence – Complete Guide

python-replace-all-character-from-string-and-pandas-column

In this article, we will explore different methods and techniques of text manipulation to replace all specific character or white spaces or special characters from string in Python. We will also implement those techniques for pandas dataframe also.

Replace all instances of character in string

In Python, you can use str.replace() method to replace all instances of a character in a string. The str.replace() method takes two arguments: the substring you want to replace and the substring you want to replace it with.

Below is an example code to replace all instances of a character in a string:

original_string = "Hello, world!"
character_to_replace = "l"
replacement_character = "X"

new_string = original_string.replace(character_to_replace, replacement_character)

print(new_string)

In this example, all occurrences of the character “l” in the original_string are replaced with the character “X” to create the new_string. You can see the output below.

HeXXo, worXd!

Python replace all special characters

the replace_special_characters function defines a string special_characters containing the special characters you want to replace. It then iterates over each character in the input string using a list comprehension.

If a character is alphanumeric (a letter or digit) or a whitespace character, it will be unchanged in the new_string. Otherwise, it is replaced with the specified replacement string (in this case, a space).

def replace_special_characters(input_string, replacement_string=''):
    special_characters = "!@#$%^&*()_+{}[]|\:;<>,.?/~`"
    
    new_string = ''.join([c if c.isalnum() or c.isspace() else replacement_string for c in input_string])
    
    return new_string

original_string = "Hello, @world! How's it going?"
replacement = ' '

new_string = replace_special_characters(original_string, replacement)
print(new_string)
Hello   world  How s it going

Replace all special characters with regex

To replace all special characters occurrence in a string, you can also use regular expressions (regex) along with the re module in Python.

import re

def replace_special_characters(input_string, replacement_string=''):
    # Define a regular expression pattern to match special characters
    pattern = r'[^\w\s]'
    
    # Use re.sub() to replace all occurrences of the pattern with the replacement string
    new_string = re.sub(pattern, replacement_string, input_string)
    
    return new_string

original_string = "Hello, @world! How's it going?"
replacement = ' '

new_string = replace_special_characters(original_string, replacement)
print(new_string)
Hello   world  How s it going 

Replace all whitespace with single space

If you know the exact number of spaces you want to replace with, then you can use below simple code with string.replace function:

string_with_whitespace = "Replace  all    whitespace  with single space in    Python"
# Replace double space with single space
string_with_whitespace.replace("  ", " ")
'Replace all  whitespace with single space in  Python'

As you can see, some of those whitespaces are still available. This is because those are three spaces but we tried to replace all double space.

Also Read:  5 Best Book for Learning Python

To overcome this issue below is the generic Python code to replace all whitespace with single space (you can change it to “_“, “:“, etc.). It will work for any number of white spaces.

def replace_whitespace_with_space(input_string):
    # Replace all whitespace sequences with a single space
    new_string = ' '.join(input_string.split())
    return new_string

original_string = "Replace  all    whitespace  with single space in    Python"
new_string = replace_whitespace_with_space(original_string)
print(new_string)
Replace all whitespace with single space in Python

In this example, the replace_whitespace_with_space function takes an input string and uses the split() method to split the string into a list of words based on whitespace. You can then use ' '.join() to join the words back together with a single space separator.

Python replace all nan with 0

To replace all occurrences of “nan” with “0” in a string, you can use the str.replace() method in Python.

def replace_nan_with_zero(input_string):
    new_string = input_string.replace("nan", "0")
    return new_string

original_string = "The value is nan, another nan value here."
new_string = replace_nan_with_zero(original_string)
print(new_string)
The value is 0, another 0 value here.

You can use the same Python function to replace all 0 with ‘nan’, 1, ‘null’, ‘na’, single quote to double quote, and so on. You just need to change values in input_string.replace function.

Pandas replace for all columns values

If you want to replace values in all columns of a Pandas DataFrame, you can use the DataFrame.replace() method.

import pandas as pd

# Create a sample DataFrame
data = {
    'A': ['foo', 'bar', 'foo', 'nan'],
    'B': ['xyz', 'nan', 'xyz', 'baz'],
    'C': ['nan', 'bar', 'foo', 'baz']
}

df = pd.DataFrame(data)

print('Input Dataframe')
print(df)

# Replace 'nan' with '0' in all columns
df_replaced = df.replace('nan', '0')

print('\n')
print('Output Dataframe')
print(df_replaced)
Input Dataframe
     A    B    C
0  foo  xyz  nan
1  bar  nan  bar
2  foo  xyz  foo
3  nan  baz  baz


Output Dataframe
     A    B    C
0  foo  xyz    0
1  bar    0  bar
2  foo  xyz  foo
3    0  baz  baz

In this example, we create a sample data frame df with three columns: ‘A’, ‘B’, and ‘C’. We then use the replace() method on the DataFrame to replace all occurrences of the string ‘nan’ with ‘0’ in all columns.

Also Read:  Subset Pandas Dataframe by Column Value

Pandas replace all columns names

If you want to replace all column names (headers) of a Pandas DataFrame with new names, you can directly assign a new list of column names to the .columns attribute.

import pandas as pd

# Create a sample DataFrame
data = {
    'A': [1, 2, 3],
    'B': [4, 5, 6],
    'C': [7, 8, 9]
}

df = pd.DataFrame(data)

print('Input Dataframe')
print(df)

# Define new column names
new_column_names = ['X', 'Y', 'Z']

# Replace all column names
df.columns = new_column_names

print('\n')
print('Output Dataframe')
print(df)
Input Dataframe
   A  B  C
0  1  4  7
1  2  5  8
2  3  6  9


Output Dataframe
   X  Y  Z
0  1  4  7
1  2  5  8
2  3  6  9

Conclusion

In this article, I shared all possible methods to explain replace all functionalities of Python, and all the code I have written in Python 3. If you want to use Python 2 you may need to do some modification.

This is it for this article, If you have any questions or suggestions or if you want me to add some other points in this article, please let me know in the comment section below.

Leave a comment