How to replace all numbers (with letters/symbols attached, i.e. 43$) in a dataframe column?

Viewed 229

I have a dataframe of online comments related to the stock market. Here's an example:

df = pd.DataFrame({'id': [1, 2, 3], 
                   'comment': ["I made $425",
                               "I got mine at 42c. per share", 
                               "Stocks saw a 12% increase"]})

I would like to replace all numbers in the dataframe (including the symbols and letters) with NUMBER to achieve:

"I made NUMBER",
"I got mine at NUMBER per share", 
"Stocks saw a NUMBER increase"

I found a close solution in a previous comment, but this solution still leaves me with the remaining letters and symbols.

def repl(x):
    return re.sub(r'\d+', lambda m: "NUMBER", x)

repl("I made 428c with a 52% increase")

>> I made NUMBERc with a NUMBER% increase

Any help would be appreciated, thanks.

3 Answers

This should work:

import re

def repl(x):
    return re.sub(r'\S*\d+\S*', lambda m: "NUMBER", x)

print(repl("I made 428c with a 52% increase"))

Output:

I made NUMBER with a NUMBER increase

Try this

def repl(l):
    s=""
    for i in l.split():
        if any([str(_) in i for _ in range(11)]):
            s+="Number"+' '
        else:
            s+=i+' '
    return s.strip()

You can use a [^\d\s]*\d\S* regex to match any chunk of 0 or more chars other than digit and whitespace, then a digit, and then any amount of non-whitespace chars, and replace with NUMBER using a vectorized Series.str.replace method.

See a Pandas test:

import pandas as pd
df = pd.DataFrame({'id': [1, 2, 3], 
                   'comment': ["I made $425",
                               "I got mine at 42c. per share", 
                               "Stocks saw a 12% increase"]})
df['comment'] = df['comment'].str.replace(r'[^\s\d]*\d\S*', 'NUMBER')
df
# =>    id                         comment
# => 0   1                   I made NUMBER
# => 1   2  I got mine at NUMBER per share
# => 2   3    Stocks saw a NUMBER increase

See the regex demo, too. Details:

  • [^\d\s]* - zero or more (*) occurrences of any char but a digit and whitespace ([^\d\s] is a negated character class)
  • \d - any digit char
  • \S* - zero or more non-whitespace chars.
Related