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

Stop code execution if a certain value is not present in a column

I want only certain values to be filled in a column (from a list A-H) and if the value is not present in that list then the code should thrown an error and stop executing further.


res = APAC['colA'].isin(['A','B','C','D','E','F','G','H'])
try:
    for i in res:
        if i == False:
            print('Different response than present in the list')
except Exception as e:
       print(e)
       raise

In short if colA contains any other values other than A,B,C,D,E,F,G,H then thrown an error and stop code from executing further.

Using the code above, I am not able to stop code execution. Help

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 :

You can raise an exception instead (or in addition) to printing it. You also don’t need to catch it

res = APAC['colA'].isin(['A','B','C','D','E','F','G','H'])
for i, val in enumerate(res):
    if not val:
        raise Exception(f'Different response than present in the list in row {i}')

Output in case of failure:

Traceback (most recent call last):
  File "test.py", line 19, in <module>
    raise Exception('Different response than present in the list')
Exception: Different response than present in the list in row 6
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