1

I am creating a function which calulates an outcome depending on parameters in the formula.

Separately to the first if-function I'd like to add a second condition for cases in which n is not None

def target_n(data, y, q = None, s = 1, n = None, ascending = True):
        if q is not None:
            target = quantile(data, q, s)
            return(target)
        else:
            target = y(data,s)
            return(target)
        if n is not None:
            sort = target.sort(tst.columns[0],ascending = ascending).ix[0:n,:]
        return(sort)

Nevertheless the function returns "target" and not "sort" for cases in which n is not None. How can I implement this?

1 Answer 1

1

Your problem is that you have a return statement whether q is None or not. As soon as the execution reaches the return statement, it exits the function.

You could do something like this

def target_n(data, y, q = None, s = 1, n = None, ascending = True):
    if q is not None:
        target = quantile(data, q, s)
    else:
        target = y(data,s)
    if n is not None:
        target = target.sort(tst.columns[0],ascending = ascending).ix[0:n,:]

    return(target)
Sign up to request clarification or add additional context in comments.

Comments

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.