Skip to main content

Python Pandas Find And Replace String Values With New Values In DataFrame Columns (Example)

Find and replace string values in a column - pandas dataframe
String type has str.replace() method which can used for finding and replacing values.We are required to chain this method string type values and pass parameters for value to be searched and new replacing value.
Based on the dataset one might be required to run search and replace on columns with mixed datatypes ie. int ,etc. To handle this assert type as string and then chain required string methods


Creating a new dataframe with dictionary 


# importing pandas
import pandas as pd


# animal_sp_char_df - with special characters
animal_data_with_sp_char = {
    "Name": ["Cat", "Dog", "Cow", "Tiger", "Goat", "Snake"],
    "Sound": ["#Meow###", "Wo##of", "Mo#oo", "Rwaar###", "##Baaa", "Skkk##sss"],
    "Mixed": [123, "#13", "53###", 321, "###456", "#3#2#1#"],
}

# creating a dataframe using the animal_data_ext animal_sp_char_df
animal_sp_char_df = pd.DataFrame(animal_data_with_sp_char)

# printing animal_sp_char_df
print("animal_sp_char_df \n", animal_sp_char_df)

animal_sp_char_df Name Sound Mixed 0 Cat #Meow### 123 1 Dog Wo##of #13 2 Cow Mo#oo 53### 3 Tiger Rwaar### 321 4 Goat ##Baaa ###456 5 Snake Skkk##sss #3#2#1#


Finding and replacing all values of a dataframe column which has only string cell contents


# create a new column - insert cleared values from Sound column
# string replace method
animal_sp_char_df["cleared_sound"] = animal_sp_char_df["Sound"].str.replace("#", "")

# printing animal_sp_char_df
print("animal_sp_char_df \n", animal_sp_char_df)

animal_sp_char_df Name Sound Mixed cleared_sound 0 Cat #Meow### 123 Meow 1 Dog Wo##of #13 Woof 2 Cow Mo#oo 53### Mooo 3 Tiger Rwaar### 321 Rwaar 4 Goat ##Baaa ###456 Baaa 5 Snake Skkk##sss #3#2#1# Skkksss


For operating columns with mixed data types use astype (".astype(str)") and then chain the string replace method.


# create a new column - insert cleared values from Mixed column
# assert the type as string using astype and then chain string replace method
animal_sp_char_df["cleared_mixed"] = (
    animal_sp_char_df["Mixed"].astype(str).str.replace("#", "")
)

# printing animal_sp_char_df
print("animal_sp_char_df \n", animal_sp_char_df)

animal_sp_char_df Name Sound Mixed cleared_sound cleared_mixed 0 Cat #Meow### 123 Meow 123 1 Dog Wo##of #13 Woof 13 2 Cow Mo#oo 53### Mooo 53 3 Tiger Rwaar### 321 Rwaar 321 4 Goat ##Baaa ###456 Baaa 456 5 Snake Skkk##sss #3#2#1# Skkksss 321

Comments

Topics

Show more