Trying to create grouped variable in python

后端 未结 4 1240
别那么骄傲
别那么骄傲 2020-12-19 23:01

I have a column of age values that I need to convert to age ranges of 18-29, 30-39, 40-49, 50-59, 60-69, and 70+:

For an example of some of the data in df \'file\',

4条回答
  •  半阙折子戏
    2020-12-19 23:49

    A friend came up with this response offline that works: def age_buckets(x): if x < 30: return '18-29' elif x < 40: return '30-39' elif x < 50: return '40-49' elif x < 60: return '50-59' elif x < 70: return '60-69' elif x >=70: return '70+' else: return 'other'

    file['agerange'] = file.age.apply(age_buckets)
    

    Thanks to everyone who took a crack at this!

提交回复
热议问题