title: Python Pandas selecting rows in a dataframe based on the relative values of other fields
body: <p>I have a dataframe that looks like this:</p>
  <pre><code>df = pd.DataFrame({'ID': ['001', '001', '002', '002'],
   'Flag': ['Y', 'N', 'N', 'Y'],
   'Snapshot Month': ['05', '06', '01', '02']})
  </code></pre>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>ID (not unique)</th>
  <th>Flag (Y/N)</th>
  <th>Snapshot Month (unique for each ID)</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>0001</td>
  <td>Y</td>
  <td>05</td>
  </tr>
  <tr>
  <td>0001</td>
  <td>N</td>
  <td>06</td>
  </tr>
  <tr>
  <td>0002</td>
  <td>N</td>
  <td>01</td>
  </tr>
  <tr>
  <td>0002</td>
  <td>Y</td>
  <td>02</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>Data from all months are aggregated to one dataframe, so the IDs are not unique, and months range from 01 to 12 (01-12 are all included; I left out most of the months for brevity). The flag variable can only go from <code>Y</code> to <code>N</code>, not the other way around. Furthermore, we can assume the flag variable can only change once.</p>
  <p>There are errors in the data. For example, ID 0002 is illegal, as it goes from <code>N</code> to <code>Y</code> chronologically.</p>
  <p>I want to be able to find out IDs corresponding to those data errors.</p>
  <p>What I have tried is to find a dataframe consisting of <code>Y</code>'s, and <code>N</code>'s, and find the ID's in common, and go into the rows themselves to see errors has occurred. But this method is not only inefficient but also impossible to scale as the data becomes large.</p>
  <p>Since the snapshot month ranges from 01 - 12 (all data come from the same year), I computed a dataframe consisting of <code>Y</code>'s with snapshot month of 12, and checked to see if they have any <code>N</code>'s in months other than 12. However this also is too manual and does not find all answers. I wonder if there are some clever ways to use the snapshot month.</p>