1conditions = [
2 (df["age"].lt(10)),
3 (df["age"].ge(10) & df["age"].lt(20)),
4 (df["age"].ge(20) & df["age"].lt(30)),
5 (df["age"].ge(30) & df["age"].lt(50)),
6 (df["age"].ge(50)),
7]
8choices = ["baby", "kid", "young", "mature", "grandpa"]
9
10df["elderly"] = np.select(conditions, choices)
11
12# Results in:
13# name age preTestScore postTestScore elderly
14# 0 Jason 42 4 25 mature
15# 1 Molly 52 24 94 grandpa
16# 2 Tina 36 31 57 mature
17# 3 Jake 24 2 62 young
18# 4 Amy 73 3 70 grandpa
19