0

I'm trying to replace special characters in a data frame with unaccented or different ones.

I can replace one with

df['col_name'] = df.col_name.str.replace('?','j') 

this turned the '?' to 'j' - but - I can't seem to figure out how to change more than one..

I have a list of special characters that I want to change. I've tried using a dictionary but it doesn't seem to work

the_reps = {'?','j'}

df1 = df.replace(the_reps, regex = True)

this gave me the error nothing to replace at position 0

EDIT:

this is what worked - although it is probably not that pretty:

df[col]=df.col.str.replace('old char','new char')
df[col]=df.col.str.replace('old char','new char')
df[col]=df.col.str.replace('old char','new char')
df[col]=df.col.str.replace('old char','new char')...

for each one ..

7
  • 1
    Try with df['column'] = df['column'].str.replace('.*@+.*', 'replacement') . I found: stackoverflow.com/questions/34702338/… Commented Dec 21, 2021 at 12:42
  • Does this answer your question? Replace Multiple Values of columns Commented Dec 21, 2021 at 12:50
  • I think where I am having an issue is - I want to replace certain characters with specific replacements - not just all of them with the same or a space or something. Commented Dec 21, 2021 at 13:02
  • Could you check this answer @PorscheAdams Commented Dec 21, 2021 at 13:07
  • I still get the error nothing to replace at position 0 - Commented Dec 21, 2021 at 13:13

3 Answers 3

1
import re
s=re.sub("[_list of special characters_]","",_your string goes here_)
print(s)

An example for this..

str="Hello$@& Python3$"
import re
s=re.sub("[$@&]","",str)
print (s)
#Output:Hello Python3

Explanation goes here..

s=re.sub("[$@&]","",s)
  • Pattern to be replaced → “[$@&]”
  • [] used to indicate a set of characters
  • [$@&] → will match either $ or @ or &
  • The replacement string is given as an empty string
  • If these characters are found in the string, they’ll be replaced with an empty string
Sign up to request clarification or add additional context in comments.

Comments

0

you can use Series.replace with a dictionary

#d = { 'actual character ':'replacement ',...}

df.columns = df.columns.to_series().replace(d, regex=True)

1 Comment

gave me an error : nothing to replace at position 0 -
0

Try This:

import re
my_str = "hello Fayzan-Bhatti Ho~!w"
my_new_string = re.sub('[^a-zA-Z0-9 \n\.]', '', my_str)
print my_new_string

Output: hello FayzanBhatti How

2 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
i'm trying to replace the special characters with specific chars - not just remove them - so more like: this is ?ust gra$$ - to - this is just grass

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.