equivalent of case when in r in pandas

Solutions on MaxInterview for equivalent of case when in r in pandas by the best coders in the world

showing results for - "equivalent of case when in r in pandas"
Héloïse
09 Jul 2019
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
similar questions
queries leading to this page
equivalent of case when in r in pandas