close

How to remove non-alpha-numeric characters from strings within a dataframe column in Python?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to remove non-alpha-numeric characters from strings within a dataframe column in Python in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

How to remove non-alpha-numeric characters from strings within a dataframe column in Python?

  1. How to remove non-alpha-numeric characters from strings within a dataframe column in Python?

    Since you wrote alphanumeric, you need to add 0-9 in the regex. But maybe you only wanted alphabetic…

  2. remove non-alpha-numeric characters from strings within a dataframe column in Python

    Since you wrote alphanumeric, you need to add 0-9 in the regex. But maybe you only wanted alphabetic…

Method 1

Use str.replace.

df
  strings
0  a#bc1!
1   a(b$c

df.strings.str.replace('[^a-zA-Z]', '')
0    abc
1    abc
Name: strings, dtype: object

To retain alphanumeric characters (not just alphabets as your expected output suggests), you’ll need:

df.strings.str.replace('\W', '')
0    abc1
1     abc
Name: strings, dtype: object 

Method 2

Since you wrote alphanumeric, you need to add 0-9 in the regex. But maybe you only wanted alphabetic…

import pandas as pd

ded = pd.DataFrame({'strings': ['a#bc1!', 'a(b$c']})

ded.strings.str.replace('[^a-zA-Z0-9]', '')

But it’s basically what COLDSPEED wrote

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read