Trying to create grouped variable in python

后端 未结 4 1236
别那么骄傲
别那么骄傲 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:58

    It looks like you are using the Pandas library. They include a function for doing this: http://pandas.pydata.org/pandas-docs/version/0.16.0/generated/pandas.cut.html

    Here's my attempt:

    import pandas as pd
    
    ages = pd.DataFrame([81, 42, 18, 55, 23, 35], columns=['age'])
    
    bins = [18, 30, 40, 50, 60, 70, 120]
    labels = ['18-29', '30-39', '40-49', '50-59', '60-69', '70+']
    ages['agerange'] = pd.cut(ages.age, bins, labels = labels,include_lowest = True)
    
    print(ages)
    
       age agerange
    0   81      70+
    1   42    40-49
    2   18    18-29
    3   55    50-59
    4   23    18-29
    5   35    30-39
    

提交回复
热议问题