Can I use pandas.dataframe.isin() with a numeric tolerance parameter?
<p>I reviewed the following posts beforehand. Is there a way to use DataFrame.isin() with an approximation factor or a tolerance value? Or is there another method that could?</p>

<p><a href="http://stackoverflow.com/questions/12065885/how-to-filter-the-dataframe-rows-of-pandas-by-within-in">How to filter the DataFrame rows of pandas by &quot;within&quot;/&quot;in&quot;?</a></p>

<p><a href="http://stackoverflow.com/questions/12096252/use-a-list-of-values-to-select-rows-from-a-pandas-dataframe">use a list of values to select rows from a pandas dataframe</a></p>

<p>EX)</p>

<pre><code>df = DataFrame({'A' : [5,6,3.3,4], 'B' : [1,2,3.2, 5]})

In : df
Out:
   A    B
0  5    1
1  6    2
2  3.3  3.2
3  4    5  

df[df['A'].isin([3, 6], tol=.5)]

In : df
Out:
   A    B
1  6    2
2  3.3  3.2
</code></pre>

<p>You can do a similar thing with <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.isclose.html" rel="nofollow">numpy's isclose</a>:</p>

<pre><code>df[np.isclose(df['A'].values[:, None], [3, 6], atol=.5).any(axis=1)]
Out: 
     A    B
1  6.0  2.0
2  3.3  3.2
</code></pre>

<hr>

<p>np.isclose returns this:</p>

<pre><code>np.isclose(df['A'].values[:, None], [3, 6], atol=.5)
Out: 
array([[False, False],
       [False,  True],
       [ True, False],
       [False, False]], dtype=bool)
</code></pre>

<p>It is a pairwise comparison of <code>df['A']</code>'s elements and <code>[3, 6]</code> (that's why we needed <code>df['A'].values[: None]</code> - for broadcasting). Since you are looking for whether it is close to any one of them in the list, we call <code>.any(axis=1)</code> at the end.</p>

<hr>

<p>For multiple columns, change the slice a little bit:</p>

<pre><code>mask = np.isclose(df[['A', 'B']].values[:, :, None], [3, 6], atol=0.5).any(axis=(1, 2))
mask
Out: array([False,  True,  True, False], dtype=bool)
</code></pre>

<p>You can use this mask to slice the DataFrame (i.e. <code>df[mask]</code>)</p>

<hr>

<p>If you want to compare <code>df['A']</code> and <code>df['B']</code> (and possible other columns) with different vectors, you can create two different masks:</p>

<pre><code>mask1 = np.isclose(df['A'].values[:, None], [1, 2, 3], atol=.5).any(axis=1)
mask2 = np.isclose(df['B'].values[:, None], [4, 5], atol=.5).any(axis=1)
mask3 = ...
</code></pre>

<p>Then slice:</p>

<pre><code>df[mask1 &amp; mask2]  # or df[mask1 &amp; mask2 &amp; mask3 &amp; ...]
</code></pre>

python pandas comparison floating-accuracy comparison-operators