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