1

Overview

I'm using pandas.DataFrame.assign to create a new column using a function in a similar way to this:

def find_break(x):

    date_is_before = df.index < datetime.now()
    under_price = df.Low < (x["Cluster Min"] - false_break)
    all_are_true = date_is_before & under_price
    if (all_are_true).any():
        return df[all_are_true].index[0]


dfClustersSupport = dfClusters.assign(
                    support_broken=dfClusters.apply(find_break, 1))

I want to now pass the thresh variable as follows:

def find_break(x, thresh ):

    false_break = thresh / 2

    date_is_before = df.index < datetime.now()
    under_price = df.Low < (x["Cluster Min"] - false_break)
    all_are_true = date_is_before & under_price
    if (all_are_true).any():
        return df[all_are_true].index[0]

Question

How do I pass the thresh variable when I apply the function here?

dfClustersSupport = dfClusters.assign(
                    support_broken=dfClusters.apply(find_break, 1))

1 Answer 1

1

Add kwargs parameter to DataFrame.apply:

dfClustersSupport = dfClusters.assign(
                     support_broken=dfClusters.apply(find_break, thresh=10, axis=1))
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. If I have multiple parameters do I add them to a list? i.e. [thresh=10, newvar, 5]
@ade1e - not sure about this combinations, is possible use also args parameter by tuples - close .apply(find_break, thresh=10, args = (newvar, 5), axis=1)

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.