3

I am attempting to search for matching values within a range within a given uncertainty in a pandas dataframe. For instance, if I have a dataframe:

    A     B        C
0   12  12.6    111.20
1   14  23.4    112.20
2   16  45.6    112.30
3   18  56.6    112.40
4   27  34.5    121.60
5   29  65.2    223.23
6   34  45.5    654.50
7   44  65.6    343.50

How can I search for a value that matches 112.6 +/-0.4 without having to create a long and difficult criteria like:

TargetVal_Max= 112.6+0.4
TargetVal_Min= 112.6-0.4

Basically, I want to create a "buffer window" that allows for all values matching a window to be returned back. I have uncertainties package, but have yet to get it working like this.

Optimally, I'd like to be able to return all index values that match a value in both C and B within a given error range.

Edit

As pointed out by @MaxU, the np.isclose f(x) works very well if you know the exact number. But is it possible to match a list of values, such that if I had a second dataframe and wanted to see if the values in C from one matched the values of C (second dataframe) within a tolerance? I have attempted to get them into a list and do it this way, but I am getting problems when attempting to do it for more than a single value at a time.

TEST= Dataframe_2["C"]
HopesNdreams = sample[sample["C"].apply(np.isclose,b=TEST, atol=1.0)]

Edit 2

I found through trying a couple of different work arounds that I can just do:

TEST1= Dataframe_2["C"].tolist
for i in TEST1:
    HopesNdreams= sample[sample["C"].apply(np.isclose,b=i, atol=1.0)]

And this returns the hits for the given column. Using the logic set forth in the first answer, I think this will work very well for what I need it to. Are there any hangups that I don't see with this method?

Cheers and thanks for the help!

Jmegan042
  • 251
  • 5
  • 15

2 Answers2

3

IIUC you can use np.isclose() function:

In [180]: df[['B','C']].apply(np.isclose, b=112.6, atol=0.4)
Out[180]:
       B      C
0  False  False
1  False   True
2  False   True
3  False   True
4  False  False
5  False  False
6  False  False
7  False  False

In [181]: df[['B','C']].apply(np.isclose, b=112.6, atol=0.4).any(1)
Out[181]:
0    False
1     True
2     True
3     True
4    False
5    False
6    False
7    False
dtype: bool

In [182]: df[df[['B','C']].apply(np.isclose, b=112.6, atol=0.4).any(1)]
Out[182]:
    A     B      C
1  14  23.4  112.2
2  16  45.6  112.3
3  18  56.6  112.4
MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419
  • This is GREAT thanks @MaxU! However, is there a way that I can match a list of values? Such that b= a series from another dataframe instead of a single number; find a number that is close to a value in that series? I attempted this both as a list generated from a series and a df column name and got "unhashable type: 'numpy.ndarray'" – Jmegan042 Jan 17 '17 at 00:40
1

Use Series.between():

df['C'].between(112.6 + .4, 112.6 - .4)
K3---rnc
  • 6,717
  • 3
  • 31
  • 46