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

Viewed 39

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?

2 Answers

Wrap the check + processing in a function that you call multiple times.

Something like

def process(df, colname, fun):
    if colname in df.columns:
        df[colname].apply(fun)

process(df, 'column_a', lambda x: bool(int(x)))
process(df, 'column_b', lambda x: 3.1416 * x)

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)
Related