title: case_when function from R to Python
body: <p>How I can implement the case_when function of R in a python code? </p>
  
  <p>Here is the case_when function of R:</p>
  
  <p><a href="https://www.rdocumentation.org/packages/dplyr/versions/0.7.8/topics/case_when" rel="noreferrer">https://www.rdocumentation.org/packages/dplyr/versions/0.7.8/topics/case_when</a> </p>
  
  <p>as a minimum working example suppose we have the following dataframe (python code follows): </p>
  
  <pre><code>import pandas as pd
  import numpy as np
  
  data = {'name': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'], 
          'age': [42, 52, 36, 24, 73], 
          'preTestScore': [4, 24, 31, 2, 3],
          'postTestScore': [25, 94, 57, 62, 70]}
  df = pd.DataFrame(data, columns = ['name', 'age', 'preTestScore', 'postTestScore'])
  df
  </code></pre>
  
  <p>Suppose than we want to create an new column called 'elderly' that looks at the 'age' column and does the following:  </p>
  
  <pre><code>if age &lt; 10 then baby
   if age &gt;= 10 and age &lt; 20 then kid 
  if age &gt;=20 and age &lt; 30 then young 
  if age &gt;= 30 and age &lt; 50 then mature 
  if age &gt;= 50 then grandpa 
  </code></pre>
  
  <p>Can someone help on this ? </p>