Removing signs and repeating numbers

Viewed 110

I want to remove all signs from my dataframe to leave it in either one of the two formats: 100-200 or 200

So the salaries should either have a single hyphen between them if a range of salaries if given, otherwise a clean single number.

I have the following data:

import pandas as pd
import re
df = {'salary':['£26,768 - £30,136/annum Attractive benefits package',
           '£26,000 - £28,000/annum plus bonus',
           '£21,000/annum',
           '£26,768 - £30,136/annum Attractive benefits package',
           '£33/hour', 
           '£18,500 - £20,500/annum Inc Bonus - Study Support + Bens',
           '£27,500 - £30,000/annum £27,500 to £30,000 + Study',
           '£35,000 - £40,000/annum',
           '£24,000 - £27,000/annum Study Support (ACCA / CIMA)',
           '£19,000 - £24,000/annum Study Support',
           '£30,000 - £35,000/annum', 
           '£44,000 - £66,000/annum + 15% Bonus + Excellent Benefits. L',
           '£75 - £90/day £75-£90 Per Day']}
data = pd.DataFrame(df)

Here's what I have tried to remove some of the signs:

salary = []
for i in data.salary:
    space = re.sub(" ",'',i)
    lower = re.sub("[a-z]",'',space)
    upper = re.sub("[A-Z]",'',lower)
    bracket = re.sub("/",'',upper)
    comma = re.sub(",", '', bracket)
    plus = re.sub("\+",'',comma)
    percentage = re.sub("\%",'', plus)
    dot = re.sub("\.",'', percentage)
    bracket1 = re.sub("\(",'',dot)
    bracket2 = re.sub("\)",'',bracket1)
    salary.append(bracket2)

Which gives me:

'£26768-£30136',
 '£26000-£28000',
 '£21000',
 '£26768-£30136',
 '£33',
 '£18500-£20500-',
 '£27500-£30000£27500£30000',
 '£35000-£40000',
 '£24000-£27000',
 '£19000-£24000',
 '£30000-£35000',
 '£44000-£6600015',
 '£75-£90£75-£90'

However, I have some repeating numbers, essentially I want anything after the first range of values removed, and any sign besides the hyphen between the two numbers.

Expected output:

 '26768-30136',
 '26000-28000',
 '21000',
 '26768-30136',
 '33',
 '18500-20500',
 '27500-30000',
 '35000-40000',
 '24000-27000',
 '19000-24000',
 '30000-35000',
 '44000-66000',
 '75-90
4 Answers

Another way using pandas.Series.str.partition with replace:

data["salary"].str.partition("/")[0].str.replace("[^\d-]+", "", regex=True)

Output:

0     26768-30136
1     26000-28000
2           21000
3     26768-30136
4              33
5     18500-20500
6     27500-30000
7     35000-40000
8     24000-27000
9     19000-24000
10    30000-35000
11    44000-66000
12          75-90
Name: 0, dtype: object

Explain:

It assumes that you are only interested in the parts upto /; it extracts everything until /, than removes anything but digits and hypen

You can use

data['salary'].str.split('/', n=1).str[0].replace('[^\d-]+','', regex=True)
# 0     26768-30136
# 1     26000-28000
# 2           21000
# 3     26768-30136
# 4              33
# 5     18500-20500
# 6     27500-30000
# 7     35000-40000
# 8     24000-27000
# 9     19000-24000
# 10    30000-35000
# 11    44000-66000
# 12          75-90

Here,

  • .str.split('/', n=1) - splits into two parts with the first / char
  • .str[0] - gets the first item
  • .replace('[^\d-]+','', regex=True) - removes all chars other than digits and hyphens.

A more precise solution is to extract the £num(-£num)? pattern and remove all non-digits/hyphens:

data['salary'].str.extract(r'£(\d+(?:,\d+)*(?:\.\d+)?(?:\s*-\s*£\d+(?:,\d+)*(?:\.\d+)?)?)')[0].str.replace(r'[^\d-]+', '', regex=True)

Details:

  • £ - a literal char
  • \d+(?:,\d+)*(?:\.\d+)? - one or more digits, followed with zero or more occurrences of a comma and one or more digits and then an optional sequence of a dot and one or more digits
  • (?:\s*-\s*£\d+(?:,\d+)*(?:\.\d+)?)? - an optional occurrence of a hyphen enclosed with zero or more whitespaces (\s*-\s*), then a £ char, and a number pattern described above.

You can do it in only two regex passes. First extract the monetary amounts with a regex, then remove the thousands separators, finally, join the output by group keeping only the first two occurrences per original row.

The advantage of this solution is that is really only extracts monetary digits, not other possible numbers that would be there if the input is not clean.

(data['salary'].str.extractall(r'£([,\d]+)')[0]     # extract £123,456 digits
 .str.replace(r'\D', '', regex=True)                # remove separator
 .groupby(level=0).apply(lambda x: '-'.join(x[:2])) # join first two occurrences
)

output:

0     26768-30136
1     26000-28000
2           21000
3     26768-30136
4              33
5     18500-20500
6     27500-30000
7     35000-40000
8     24000-27000
9     19000-24000
10    30000-35000
11    44000-66000
12          75-90

You can use replace with a pattern and optional capture groups to match the data format, and use those groups in the replacement.

import pandas as pd

df = {'salary':['£26,768 - £30,136/annum Attractive benefits package',
                '£26,000 - £28,000/annum plus bonus',
                '£21,000/annum',
                '£26,768 - £30,136/annum Attractive benefits package',
                '£33/hour',
                '£18,500 - £20,500/annum Inc Bonus - Study Support + Bens',
                '£27,500 - £30,000/annum £27,500 to £30,000 + Study',
                '£35,000 - £40,000/annum',
                '£24,000 - £27,000/annum Study Support (ACCA / CIMA)',
                '£19,000 - £24,000/annum Study Support',
                '£30,000 - £35,000/annum',
                '£44,000 - £66,000/annum + 15% Bonus + Excellent Benefits. L',
                '£75 - £90/day £75-£90 Per Day']}
data = pd.DataFrame(df).salary.replace(
    r"^£(\d+)(?:,(\d+))?(?:\s*(-)\s*£(\d+)(?:,(\d+))?)?/.*",
    r"\1\2\3\4\5", regex=True
)
print(data)

The pattern matches

  • ^ Start of string
  • £ Match literally
  • (\d+) Capture 1+ digits in group 1
  • (?:,(\d+))?Optionally capture 1+ digits in group 2 that is preceded by a comma to match the data format
  • (?: Non capture group to match as a whole
    • \s*(-)\s*£ capture - between optional whitespace chars in group 3 and match £
    • (\d+)(?:,(\d+))? The same as previous, now in group 4 and group 5
  • )? Close non capture group and make it optional

See a regex demo.

Output

0     26768-30136
1     26000-28000
2           21000
3     26768-30136
4              33
5     18500-20500
6     27500-30000
7     35000-40000
8     24000-27000
9     19000-24000
10    30000-35000
11    44000-66000
12          75-90
Related