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\',
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