Pandas : Apply Merge operations from a column
Wrap the logic in a function, then .apply that function.

Consider then doing another function lookup using a simple dictionary. This keeps your functions very short and simple.


Something like:
def _op_get_a(row):
    return row.data_a

def _op_get_b(row):
    return row.data_b

def _op_avg(row):
    return (row.data_a + row.data_b) / 2.0

def _op_sum(row):
    return row.data_a + row.data_b

def _op_concat(row):
    return np.concatenate([row.data_a, row.data_b], axis=0)

def process_row(row):
    return {
        'data_a': _op_get_a,
        'data_b': _op_get_b,
        'avg': _op_avg,
        'sum': _op_sum,
        'concat': _op_concat,
    }[row.operation](row)

# The &quot;axis=1&quot; is critical to apply this row-wise rather than column-wise
dataframe['new_column'] = dataframe.apply(process_row, axis=1)

I haven't tested this code, but you get the idea. Supporting new row operations is as simple as writing it as a row-wise function and hooking it into the lookup dictionary. The code is written at the smallest possible unit and able to be individually documented without creating a big cluttered mess.