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:
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 |
df = pd.DataFrame({'name': ['Jeff', 'Esha', 'Jia'], 'age': [30, 56, 8]})
name | age | |
---|---|---|
0 | Jeff | 30 |
1 | Esha | 56 |
2 | Jia | 8 |
name | age | age_bracket | |
---|---|---|---|
0 | Jeff | 30 | 19-30 |
1 | Esha | 56 | 51-65 |
2 | Jia | 8 | 0-10 |