Horje
equivalent of case_when in r in pandas Code Example
equivalent of case_when in r in pandas
conditions = [
    (df["age"].lt(10)),
    (df["age"].ge(10) & df["age"].lt(20)),
    (df["age"].ge(20) & df["age"].lt(30)),
    (df["age"].ge(30) & df["age"].lt(50)),
    (df["age"].ge(50)),
]
choices = ["baby", "kid", "young", "mature", "grandpa"]

df["elderly"] = np.select(conditions, choices)

# Results in:
#      name  age  preTestScore  postTestScore  elderly
#  0  Jason   42             4             25   mature
#  1  Molly   52            24             94  grandpa
#  2   Tina   36            31             57   mature
#  3   Jake   24             2             62    young
#  4    Amy   73             3             70  grandpa




Python

Related
plotly express change legend labels Code Example plotly express change legend labels Code Example
exercises with classes in python with solutions Code Example exercises with classes in python with solutions Code Example
how to clear multi dictionary in python Code Example how to clear multi dictionary in python Code Example
i want to check my python code online Code Example i want to check my python code online Code Example
AttributeError: ‘str’ object has no attribute ‘decode’ Code Example AttributeError: ‘str’ object has no attribute ‘decode’ Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
10