1

How do I pass multiple parameters with apply in Pandas?

do_something is a function:

def do_something(x,test="testFoo")

This can be used with dataframe.apply

df2.apply(do_something, test="testBar",axis=1)

I want to pass another parameter (df) like this:

def do_something(x,test="testFoo",df)

How do I now call apply with this df parameter similar to this:

df2.apply(do_something, test="testBar",df=df,axis=1)
0

1 Answer 1

1

For me working small change df=df in def:

df = pd.DataFrame({'a':[1,2]})
df2 = pd.DataFrame({'g':[50,40]})

def do_something(x,test="testFoo",df=df):
    print (df)
       a
    0  1
    1  2
       a
    0  1
    1  2

df2.apply(do_something, test="testBar",df=df,axis=1)

EDIT:

df2 = pd.DataFrame({'g':[50,40]})

def get_df():
    return pd.DataFrame({'a':[1,2]})

def do_something(x,df,test="testFoo"):
    print (df)

df2.apply(do_something, test="testBar",df=get_df(),axis=1)
Sign up to request clarification or add additional context in comments.

2 Comments

I tried this and get NameError: name 'df' is not defined. I'm importing the function from another module.
@ade1e - I try edit question, important is order do_something(x,df,test="testFoo")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.