title: Replicating SAS' first and last functionality with Python
body: <p>I have recently migrated to Python as my primary tool for analysis and I am looking to be able to replicate the first. &amp; last. functionality found in SAS. The SAS code would be as follows;</p>
  
  <pre><code>data data.out;
     set data.in;
     if first.ID then flag = 1;
     if last.ID then flag = 1;
  run;
  </code></pre>
  
  <p>The output would be as follows;</p>
  
  <pre><code>ID     flag
  AAAA   1
  AAAA   0
  AAAA   0
  AAAA   1
  BBBB   1
  BBBB   0
  BBBB   0
  BBBB   1
  CCCC   1
  CCCC   0
  CCCC   1
  </code></pre>
  
  <p>Any ideas about how to do this in Python?</p>