I have a pandas dataframe with several columns, like
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randint(0,100,size=(100, 7)), columns=list('ABCDEFG'))
and I want to apply
to it a function that can accept as arguments all the columns of the dataframe:
# function would do something more complex potentially :)
def foo(a,b,c,d,e,f,g):
# do stuff with a,b,c,d,e,f,g. Here I do something silly/simple
return a + b*2 + c*3 + d*4 + e*5 + f*5 + g*5
Now, I would like to apply foo
to all rows of df
. What's the proper syntax to do so?
My attempts work
df.apply(lambda row: foo(row[0], row[1], row[2], row[3], row[4], row[5], row[6]), axis = 1) # terrible
df.apply(lambda row: foo(*row), axis = 1) # better
but is there a way to do it even more concisely, e.g. without lambda
?