close

How can I remove all non-numeric characters from all the values in a particular column in pandas dataframe?

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

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

How can I remove all non-numeric characters from all the values in a particular column in pandas dataframe?

  1. How can I remove all non-numeric characters from all the values in a particular column in pandas dataframe?

    Use str.extract and pass a regex pattern to extract just the numeric parts:
    In[40]: dfObject['C'] = dfObject['C'].str.extract('(\d+)', expand=False) dfObject

  2. remove all non-numeric characters from all the values in a particular column in pandas dataframe

    Use str.extract and pass a regex pattern to extract just the numeric parts:
    In[40]: dfObject['C'] = dfObject['C'].str.extract('(\d+)', expand=False) dfObject

Method 1

Use str.extract and pass a regex pattern to extract just the numeric parts:

In[40]:
dfObject['C'] = dfObject['C'].str.extract('(\d+)', expand=False)
dfObject

Out[40]: 
        A         B    C
1   red78    square  235
2   green    circle  123
3  blue45  triangle  657

If needed you can cast to int:

dfObject['C'] = dfObject['C'].astype(int)

Method 2

You can use .str.replace with a regex:

dfObject['C'] = dfObject.C.str.replace(r"[a-zA-Z]",'')

output:

        A         B    C
1   red78    square  235
2   green    circle  123
3  blue45  triangle  657

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