Skip to content Skip to sidebar Skip to footer

Pass Dataframe To Apply Function Pandas As Argument

Is possible to pass a DataFrame to an apply function like this? df2 = df1.apply(func,axis=1,args=df2) def func(df1,df2): # do stuff in df2 for each row of df1 return df2 T

Solution 1:

From the df.apply docs:

DataFrame.apply(func, axis=0, broadcast=False, raw=False, reduce=None, args=(), **kwds)

Applies function along input axis of DataFrame.

args : tuple

Positional arguments to pass to function in addition to the array/series.

The right way is to pass your arguments in a tuple, like this:

df1.apply(func, axis=1, args=(df2, ))

Further improvements to your code would be possible if it were known what you are trying to achieve.

Post a Comment for "Pass Dataframe To Apply Function Pandas As Argument"