Is there a way to use pandas.apply with a variable number of multiple columnar arguments? For example, say I have this data frame:
df = pd.DataFrame({'A':['a','b','c'],
'B':['a','b','c'],
'C':['a','b','c'],
'D':['a','b','c']})
I want to write a function that concatenates columns to produce a new column - very similar to this SO question. So a two column example would be:
def dynamic_concat_2(df, one, two):
return df[one]+df[two]
I use the function like so
df['concat'] = df.apply(dynamic_concat2, axis=1, one='A',two='B')
Now the difficulty that I cannot figure out is how to do this for an unknown dynamic amount of columns. Is there a way to generalize the function usings **kwargs? So it could be 1-n columns to concatenate?
Additional context: This is a simple example of a larger problem to dynamically calculate row level data. A unknown number of columns have data that specifies a query to a database - this gets fed into a query and returns a value. I've written some truly inflexible horribly un-pythonic solutions (think for loops going through each row of data) that haven't worked. I'm hoping use of a df.apply can python-ify things.