title: How to map numeric data into categories / bins in Pandas dataframe
body: <p>I've just started coding in python, and my general coding skills are fairly rusty :(     so please be a bit patient</p>
  
  <p>I have a pandas dataframe:</p>
  
  <p><a href="https://i.stack.imgur.com/HNXlY.png" rel="noreferrer"><img src="https://i.stack.imgur.com/HNXlY.png" alt="SamplePandas"></a></p>
  
  <p>It has around 3m rows.  There are 3 kinds of age_units: Y, D, W  for years, Days &amp; Weeks.  Any individual over 1 year old has an age unit of Y and my first grouping I want is &lt;2y old so all I have to test for in Age Units is Y...</p>
  
  <p>I want to create a new column AgeRange and populate with the following ranges:</p>
  
  <ul>
  <li>&lt;2 </li>
  <li>2 - 18 </li>
  <li>18 - 35 </li>
  <li>35 - 65 </li>
  <li>65+</li>
  </ul>
  
  <p>so I wrote a function</p>
  
  <pre><code>def agerange(values):
      for i in values:
          if complete.Age_units == 'Y':
              if complete.Age &gt; 1 AND &lt; 18 return '2-18'
              elif complete.Age &gt; 17 AND &lt; 35 return '18-35'
              elif complete.Age &gt; 34 AND &lt; 65 return '35-65'
              elif complete.Age &gt; 64 return '65+'
          else return '&lt; 2'
  </code></pre>
  
  <p>I thought if I passed in the dataframe as a whole I would get back what I needed  and then could create the column I wanted something like this:</p>
  
  <pre><code>agedetails['age_range'] = ageRange(agedetails)
  </code></pre>
  
  <p>BUT when I try to run the first code to create the function I get:</p>
  
  <pre><code>  File "&lt;ipython-input-124-cf39c7ce66d9&gt;", line 4
      if complete.Age &gt; 1 AND complete.Age &lt; 18 return '2-18'
                            ^
  SyntaxError: invalid syntax
  </code></pre>
  
  <p>Clearly it is not accepting the AND - but I thought I heard in class I could use AND like this?  I must be mistaken but then what would be the right way to do this?</p>
  
  <p>So after getting that error, I'm not even sure the method of passing in a dataframe will throw an error either.  I am guessing probably yes.  In which case - how would I make that work as well?</p>
  
  <p>I am looking to learn the best method, but part of the best method for me is keeping it simple even if that means doing things in a couple of steps...</p>