Colin Sorensen
Colin Sorensen

Reputation: 149

Replacing multiple values in pandas column at once

This is an incredibly basic question but after reading through Stack and the documentation for str.replace, I'm not finding the answer. Trying to drop all of the punctuation in a column. What would be the proper syntax for this?

This works but it's absurd: igog['text']=igog['text'].str.replace(",","").str.replace("/","").str.replace(".","").str.replace("\"","").str.replace("?","").str.replace(";","")

This doesn't: igog['text'] = igog['text'].str.replace({","," ","/","",".","","\"","","?","",";",""}).

Because I keep getting "replace() missing 1 required positional argument: 'repl'".

Thanks in advance!

Upvotes: 0

Views: 551

Answers (1)

zoldxk
zoldxk

Reputation: 1

You can make a simple loop like this:

t=",/.\?;"
for i in t:
   igog[text]=igog[text].replace(i,"")

or you can use regex:

igog['text'].str.replace("[,/.\?;]", "")

or you can use re.sub():

import re
igog['text'] = re.sub('[,/.\?;]', "", igog['text'])

or you can define a translation table :

igog['text'].translate({ord(ch):' ' for ch in ',/.\?;'})

Upvotes: 2

Related Questions