close

How to remove numbers from string terms in a pandas dataframe

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to remove numbers from string terms in a 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.

Table of Contents

How to remove numbers from string terms in a pandas dataframe?

  1. How to remove numbers from string terms in a pandas dataframe?

    .str is not necessary. You can use pandas dataframe.replace or series.replace with regex=True argument.

  2. remove numbers from string terms in a pandas dataframe

    .str is not necessary. You can use pandas dataframe.replace or series.replace with regex=True argument.

Method 1

You can apply str.replace to the Name column in combination with regular expressions:

import pandas as pd

# Example DataFrame
df = pd.DataFrame.from_dict({'Name'  : ['May21', 'James', 'Adi22', 'Hello', 'Girl90'],
                             'Volume': [23, 12, 11, 34, 56],
                             'Value' : [21321, 12311, 4435, 32454, 654654]})

df['Name'] = df['Name'].str.replace('\d+', '')

print(df)

Output:

    Name   Value  Volume
0    May   21321      23
1  James   12311      12
2    Adi    4435      11
3  Hello   32454      34
4   Girl  654654      56

In the regular expression \d stands for “any digit” and + stands for “one or more”.

Thus, str.replace('\d+', '') means: “Replace all occurring digits in the strings with nothing”.

Method 2

.str is not necessary. You can use pandas dataframe.replace or series.replace with regex=True argument.

df.replace(‘\d+’, ”, regex=True)
if you want to change source dataframe use inplace=True.

df.replace(‘\d+’, ”, regex=True, inplace=True)

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