How to remove numbers from string terms in pandas data frame

I have a data frame similar to the one below:

Name Volume Value May21 23 21321 James 12 12311 Adi22 11 4435 Hello 34 32454 Girl90 56 654654 

I want the result to be in the format:

 Name Volume Value May 23 21321 James 12 12311 Adi 11 4435 Hello 34 32454 Girl 56 654654 

Want to remove all numbers from the Name column.

The closest I came at the cell level with the following code:

 result = ''.join([i for i in df['Name'][1] if not i.isdigit()]) 

Any idea how to do this better at the series / data level.

+18
python string pandas
source share
2 answers

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

 import pandas as pd # Create example DataFrame a = pd.DataFrame.from_dict({'Name': ['May21', 'James', 'Adi22', 'Hello', 'Girl90'], 'Volume': [23, 12, 11, 34, 56], 'Value': [21321, 12311, 4435, 32454, 654654]}) a['Name'] = a['Name'].str.replace('\d+', '') print(a) 

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 means "any digit", and + means "one or more."

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

+46
source share

You can do it like this:

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

Check here Regex demo online: https://regex101.com/r/Y6gJny/2

Anything that matches the \d+ pattern, i.e. 1 or more digits, will be replaced with an empty string.

+6
source share

All Articles