0

I have a age column:

     A     
  "25-34"
  "18-24"
    34
  "35-44"
  "+65" 
    26
    32
    70 
   ...

I wanna replace the single digits with the appropriate range of age. I am kinda newbie and I am stuck. For instance, 32 is gonna be replaced with the string "35-44" What would be the proper way to replace integers with proper range of age?

Thanks in advance.

my expected column would be:

     A     
  "25-34"
  "18-24"
  "25-34"
  "35-44"
  "+65" 
  "25-34"
  "25-34"
  "+65" 
   ... 
Fragrance
  • 44
  • 7
  • Have a look at `pd.Interval`. – Quang Hoang Aug 06 '20 at 13:05
  • 4
    What would your expected output be? What's the integer value of "35-44" for instance... it almost seems like you want that field as a "Categorical"... in which case - what you've got even as a string seems just fine anyway – Jon Clements Aug 06 '20 at 13:05
  • Ok now im Lost on what you want. It seems you yust remove all digits that are single – Noah Aug 06 '20 at 13:12
  • Yes. Sorry, if I wasn't so clear. I wanna map all single digits to appropriate range of age which is string. – Fragrance Aug 06 '20 at 13:13

1 Answers1

1
cut_labels = ['0-17', '18-34', '35-44','45-64','+65']
cut_bins = [0, 17, 34,44,64,float("inf") ]
df['A_new'] = pd.cut(df['A'], bins=cut_bins, labels=cut_labels)
oreopot
  • 3,392
  • 2
  • 19
  • 28