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)
Error:
return text.translate(None,string.punctuation)
AttributeError: 'float' object has no attribute 'translate'
I am using python 2.7. Any suggestions would be helpful.
One of the easiest ways to remove punctuation from a string in Python is to use the str. translate() method. The translate() method typically takes a translation table, which we'll do using the . maketrans() method.
Ways to Remove Punctuation Marks from a String in PythonUsing the Regex. By using the translate() method. Using the join() method.
strip() function is used to remove leading and trailing characters. Strip whitespaces (including newlines) or a set of specified characters from each string in the Series/Index from left and right sides. Equivalent to str. strip().
Using Pandas str.replace and regex:
df["new_column"] = df['review'].str.replace('[^\w\s]','')
You can build a regex using the string
module's punctuation list:
df['review'].str.replace('[{}]'.format(string.punctuation), '')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With