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

Check if all dataframe row values are in specified range

How to check for each row in dataframe if all its values are in specified range?

import pandas as pd

new = pd.DataFrame({'a': [1,2,3], 'b': [-5,-8,-3], 'c': [20,0,0]})

For instance range <-5, 5>:

>>    a  b   c
>> 0  1 -5  20  # abs(20) > 5, hence no
>> 1  2 -8   0  # abs(-8) > 5, hence no
>> 2  3 -3   0  # abs(-3) <= 5, hence yes

Solution with iteration

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

print(['no' if any(abs(i) > 5 for i in a) else 'yes' for _, a in new.iterrows()])

>> ['no', 'no', 'yes']

>Solution :

Doing:

out = (df.gt(-5) & df.lt(5)).all(axis=1)
# Or if you just want to supply a single value:
# df.abs().lt(5).all(axis=1)
print(out)

Output:

0    False
1    False
2     True
dtype: bool

You could add this as a new column, and change things to no/yes if desired (which imo is a terrible idea):

df['valid'] = np.where(df.abs().lt(5).all(1), 'yes', 'no')
print(df)

# Output:

   a  b   c valid
0  1 -5  20    no
1  2 -8   0    no
2  3 -3   0   yes
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