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?