vikky vikky - 1 year ago 262
Python Question

Remove punctuations in pandas

code: df['review'].head()
index review
output: 0 These flannel wipes are OK, but in my opinion

I want to remove punctuations from the column of the dataframe and create a new column.

code: import string
def remove_punctuations(text):
return text.translate(None,string.punctuation)

df["new_column"] = df['review'].apply(remove_punctuations)

return text.translate(None,string.punctuation)
AttributeError: 'float' object has no attribute 'translate'

I am using python 2.7. Any suggestions would be helpful.

Answer Source

Using Pandas str.replace and regex:

df["new_column"] = df['review'].str.replace('[^\w\s]','')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download