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

Is there a concise way to handle and ignore exceptions on each line of code

I need a function to apply different transformations on each of many possible columns in a DataFrame. Is there a concise way to do this I’m not thinking of? Either of my solutions,

def process_frame(frame):
   try:
      frame.column_a = frame.column_a.apply(lambda x: bool(int(x)))
   except KeyError:
      pass
   try:
      frame.column_b = frame.column_b.apply(lambda x: min(0, x))
   except KeyError:
      pass
   # etc, etc

or

def process_frame(frame):
   if 'column_a' in frame.columns:
      frame.column_a = frame.column_a.apply(lambda x: bool(int(x)))
   if 'column_b' in frame.columns:
      frame.column_b = frame.column_b.apply(lambda x: min(0, x))
   # etc, etc

are quite repetitive and verbose. Is there a more elegant way to iteratively try/except each line in a block of code?

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 create a dictionary with column names as keys and functions as values. Then you can iterate over the dictionary:

f_dict = {'column_a': lambda x: bool(int(x)), 
          'column_b': lambda x: min(0, x)}

for k, f in f_dict.items():
    if k in df.columns:
        df[k] = df[k].apply(f)
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