Pandas: map function along each row of columns defined at runtime (using *args)

Viewed 9548

I want to apply a function on the row data of a Pandas DataFrame using *args. This can be done like this (toy example to retrieve maximum of row):

df = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD'))
def f(*args):
   cols = [c for c in args]
   return max(cols)

m = list(map(f,df['A'],df['B'],df['C'],df['D']))

Is there a way to do it in this manner without having to list all the columns separately? For example, when a dataframe has arbitrary columns, defined at runtime.

Finding the maximum could also be done in easier ways but how can one apply arbitrary functions to rows (if *args is not possible)

3 Answers
Related