Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Comparing dataframe columns with acceptable deviation in Python

How can I have the comparison between two pandas dataframe rows considering an acceptable deviation instead of a 100% match?

For example:

Acceptable deviation: 10

column1 | column2 | Result
100     | 110     | True
0       | 20      | False
0       | 9       | True

I wasn’t able to find any references to built-in functions or similar scenarios.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use:

df['Result'] = abs(df['column1'] - df['column2']) <= 10
print(df)

# Output:
   column1  column2  Result
0      100      110    True
1        0       20   False
2        0        9    True

Alternative, chained methods:

df['Result'] = df['column1'].sub(df['column2']).abs().le(10)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading