Bucketing values using cut

Pandas gives functions to group values into buckets, cut and qcut.

Using the DataFrame shape,

import pandas as pd

df = pd.DataFrame({'name': ['Jeff', 'Esha', 'Jia'], 
                   'age': [30, 56, 8]})
name age
1 Jeff 30
2 Esha 56
3 Jia 8

Write a function, add_age_bracket(df) which takes in the DataFrame and returns a DataFrame with an additional column age_bracket where the brackets are:

  1. "0-10"
  2. "11-18"
  3. "19-30"
  4. "31-50"
  5. "51-65"
  6. "66-80"
  7. "81-120"

So the new DataFrame would be

name age age_bracket
1 Jeff 30 19-30
2 Esha 56 50-65
3 Jia 8 0-10

Example Input

Code to generate input

df = pd.DataFrame({'name': ['Jeff', 'Esha', 'Jia'], 
                       'age': [30, 56, 8]})


Table generated

name age
0 Jeff 30
1 Esha 56
2 Jia 8

Example Output

name age age_bracket
0 Jeff 30 19-30
1 Esha 56 51-65
2 Jia 8 0-10