Let’s see how we can remove punctuations in pandas data frames. For instance, let’s say that we are dealing with the following data frame:
import pandas as pd df = pd.DataFrame({'mytext':['I love Predictive Hacks!','How can I remove punctuations?' ,'He said: "This is cool!".']}) df
mytext
0 I love Predictive Hacks!
1 How can I remove punctuations?
2 He said: "This is cool!".
Let’s see how we can remove the punctuations. We will use the regular expression [^\w\s]
which means what ever is not a word or a space.
df['mytext'] = df['mytext'].str.replace('[^\w\s]','') df
mytext
0 I love Predictive Hacks
1 How can I remove punctuations
2 He said This is cool