title: find a value based on multiple conditions within a tolerance in Pandas
body: <p>I'm looking for an identification procedure using Pandas to trace the movement of some objects.
  I want to assign a value (ID) compared to previous frame (Time) for each group based on multiple conditions (X,Y,Z) being within a tolerance (X&lt;0.5,Y&lt;0.6,Z&lt;0.7) and write it in place. Also, if the value is not available, give it a new value (Count) which restarts by Group.
  Here is an example:</p>
  <p>Before:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th style="text-align: left;">Group</th>
  <th style="text-align: center;">Time</th>
  <th style="text-align: center;">X</th>
  <th style="text-align: center;">Y</th>
  <th style="text-align: right;">Z</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td style="text-align: left;">A</td>
  <td style="text-align: center;">1.0</td>
  <td style="text-align: center;">0.1</td>
  <td style="text-align: center;">0.1</td>
  <td style="text-align: right;">0.1</td>
  </tr>
  <tr>
  <td style="text-align: left;">A</td>
  <td style="text-align: center;">1.0</td>
  <td style="text-align: center;">2.2</td>
  <td style="text-align: center;">2.2</td>
  <td style="text-align: right;">2.2</td>
  </tr>
  <tr>
  <td style="text-align: left;">B</td>
  <td style="text-align: center;">1.0</td>
  <td style="text-align: center;">3.3</td>
  <td style="text-align: center;">3.3</td>
  <td style="text-align: right;">3.3</td>
  </tr>
  <tr>
  <td style="text-align: left;">A</td>
  <td style="text-align: center;">1.1</td>
  <td style="text-align: center;">0.4</td>
  <td style="text-align: center;">0.4</td>
  <td style="text-align: right;">0.4</td>
  </tr>
  <tr>
  <td style="text-align: left;">A</td>
  <td style="text-align: center;">1.1</td>
  <td style="text-align: center;">5.5</td>
  <td style="text-align: center;">5.5</td>
  <td style="text-align: right;">5.5</td>
  </tr>
  <tr>
  <td style="text-align: left;">B</td>
  <td style="text-align: center;">1.1</td>
  <td style="text-align: center;">3.6</td>
  <td style="text-align: center;">3.6</td>
  <td style="text-align: right;">3.6</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>After</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th style="text-align: left;">Group</th>
  <th style="text-align: center;">Time</th>
  <th style="text-align: center;">X</th>
  <th style="text-align: center;">Y</th>
  <th style="text-align: center;">Z</th>
  <th style="text-align: right;">ID</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td style="text-align: left;">A</td>
  <td style="text-align: center;">1.0</td>
  <td style="text-align: center;">0.1</td>
  <td style="text-align: center;">0.1</td>
  <td style="text-align: center;">0.1</td>
  <td style="text-align: right;">1</td>
  </tr>
  <tr>
  <td style="text-align: left;">A</td>
  <td style="text-align: center;">1.0</td>
  <td style="text-align: center;">2.2</td>
  <td style="text-align: center;">2.2</td>
  <td style="text-align: center;">2.2</td>
  <td style="text-align: right;">2</td>
  </tr>
  <tr>
  <td style="text-align: left;">B</td>
  <td style="text-align: center;">1.0</td>
  <td style="text-align: center;">3.3</td>
  <td style="text-align: center;">3.3</td>
  <td style="text-align: center;">3.3</td>
  <td style="text-align: right;">1</td>
  </tr>
  <tr>
  <td style="text-align: left;">A</td>
  <td style="text-align: center;">1.1</td>
  <td style="text-align: center;">0.4</td>
  <td style="text-align: center;">0.4</td>
  <td style="text-align: center;">0.4</td>
  <td style="text-align: right;">1</td>
  </tr>
  <tr>
  <td style="text-align: left;">A</td>
  <td style="text-align: center;">1.1</td>
  <td style="text-align: center;">5.5</td>
  <td style="text-align: center;">5.5</td>
  <td style="text-align: center;">5.5</td>
  <td style="text-align: right;">3</td>
  </tr>
  <tr>
  <td style="text-align: left;">B</td>
  <td style="text-align: center;">1.1</td>
  <td style="text-align: center;">3.6</td>
  <td style="text-align: center;">3.6</td>
  <td style="text-align: center;">3.6</td>
  <td style="text-align: right;">1</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>for clarification:
  Row#4: X,Y,Z change is within the tolerance, hence same ID
  Row#5: X,Y,Z change is NOT within the tolerance, hence new ID in Group A
  Row#6: X,Y,Z change NOT within the tolerance, hence same ID</p>
  <p>I think I can trace the movement of my objects only for one direction (Let's say X) using pd.merge_asof regardless of their group and time, and find their ID. My problems are: 1. considering the group and time, 2. assignment of new ID, 3. different tolerances.</p>
  <pre><code>df3=pd.merge_asof(df1, df2, on=&quot;X&quot;, direction=&quot;nearest&quot;, by=[&quot;Group&quot;], tolerance=0.5)
  </code></pre>