RustyShackleford
RustyShackleford

Reputation: 3667

How to replace everything but letters and numbers in pandas column?

I have a df like so:

id    answer
1      how are you? 
2      What's the word in the letters'
3      .... \xa0 \xa0' Hey what's up?

How do I replace every thing that is not a letter or number with nothing in df.answer?

New df will look like this:

id    question
1      how are you 
2      Whats the word in the letters
3      Hey whats up 

Upvotes: 0

Views: 592

Answers (2)

YOLO
YOLO

Reputation: 21709

You can also use re module:

df['answer'] = df['words'].apply(lambda x: re.sub(r'\W+&\s','',x))

Another alternative:

df['words'].str.replace(r'\W+&\s', '')

Upvotes: 0

Ashish Acharya
Ashish Acharya

Reputation: 3399

df.answer = df.apply(lambda row: ''.join(i for i in row.answer if i.isalnum() or i==' '), axis=1)

This should work.

Upvotes: 2

Related Questions