Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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
Comment

PREVIOUS NEXT
Code Example
Python :: Separating a relational plot based on a sixth variable | seaborn relational plot 
Python :: Warning message: In scan(file = file, what = what, sep = sep, quote = quote, dec = dec, : EOF within quoted string 
Python :: example of a bad code 
Python :: aiml python install 
Python :: generate fibonacci series in python 
Python :: fibonacci series program in python 
Python :: how to do fibonacci sequence in python 
Python :: install matplotlib on nvidia jetson nx 
Python :: BusyIndicator Import 
Python :: scipy get frequencies of image 
Python :: hello kitt 
Python :: how to read xlsx file from one directory above python 
Python :: access kwargs in template django 
Python :: hi guys 
Python :: get decimal value of char python 
Python :: mechanize python XE #25 
Python :: sort dictionary by values 
Python :: how to run function when file is modified python 
Python :: python clean filename 
Python :: create new model description odoo 
Python :: plotly dcc.interval bar graph with time 
Python :: pairplot legend position 
Python :: FizzBuzz in Python Using String Concatenation 
Python :: extracting bounding box from xml file python 
Python :: Convert Int to String Using F-strings 
Python :: split x and y pandas 
Python :: tree view width 
Python :: which can be reversed , string or list? 
Python :: python split respect quotes 
Python :: Python NumPy ravel function example array.ravel is equivalent to reshape(-1, order=order) 
ADD CONTENT
Topic
Content
Source link
Name
9+5 =