Predictive Hacks

How to remove punctuations in pandas

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

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

Python

Image Captioning with HuggingFace

Image captioning with AI is a fascinating application of artificial intelligence (AI) that involves generating textual descriptions for images automatically.

Python

Intro to Chatbots with HuggingFace

In this tutorial, we will show you how to use the Transformers library from HuggingFace to build chatbot pipelines. Let’s