0

Make a Date Range of -2 +5 years e.g: 31 years -> (29-36) years

My input is

     age          
0    31 years  
1    25 years  

My Output is

     age       age_range   
0    31 years  29-36 years  
1    25 years  23-30 years

1 Answers1

2

First extract numeric values and then join together with add 2 and subtract 5:

s = df['age'].str.extract('(\d+)', expand=False).astype(int)
df['age_range'] = s.sub(2).astype(str) + '-' + s.add(5).astype(str) + ' years'

Another solution, thanks @IMCoins for suggestion:

#python 3.6+
df['age_range'] = [f'{x-2}-{x + 5} years' for x in s]
#python bellow
#df['age_range'] = ['{}-{} years'.format(x-2, x+5) for x in s]

print (df)
        age    age_range
0  31 years  29-36 years
1  25 years  23-30 years

If need regular ranges then use binning.

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252