0

I have defined the function that takes two arguments and returns two values. It looks like as below:

def normalized_Value(x,RY):
    Norm = 0
    if RY >= 2016:
        if x > 3.382:
            x = 3.382 #return 3.382
            Norm = 1
        else:
            x = x
        #return x
        return x,Norm
    else:
        if x > 11.93:
            x = 11.93 #return 3.382
            Norm = 1
        else:
            x = x
        #return x
        return x,Norm

I called the function in dataframe to create two new columns in the dataframe. I used following code to call the function:

df['Normalized_val'], temp['Normalized val event'] = zip(*temp[['value','RY']].apply(normalized_Value))

However, when I ran the code it throws an error message of missing argument,

TypeError: normalized_Value() missing 1 required positional argument: 'RY'

I am passing two arguments in the code, not sure why it is throwing an error message. Could anyone help in rectifying the issue?

3
  • Something's off. By any chance, do these three names refer to the same function: normalized_Value, normalized_val and normalized_SAIDI ? Commented Jun 8, 2020 at 22:49
  • Sorry, it was wired mistake. Copied from old code. Now fixed Commented Jun 8, 2020 at 22:52
  • If you would like normalized_Value to work on two values, you could have it accept a tuple and split it internally. e.g. def normalized_Value(pair): x, RY = pair because pandas would just supply your function with a single argument. Commented Jun 8, 2020 at 22:52

1 Answer 1

1

Change your code

def normalized_Value(x,RY):
    Norm = 0
    if RY >= 2016:
        if x > 3.382:
            x = 3.382 #return 3.382
            Norm = 1
        else:
            x = x
        #return x
        return [x,Norm]
    else:
        if x > 11.93:
            x = 11.93 #return 3.382
            Norm = 1
        else:
            x = x
        #return x
        return [x,Norm]



s=pd.DataFrame(temp.apply(lambda x : normalized_Value(x['value'],x['RY']),axis=1).tolist() 
                                                               ,index = temp.index, columns=['Normalized_val','Normalized val event'])
temp=temp.join(s)
Sign up to request clarification or add additional context in comments.

3 Comments

It returns o and 1 only
@user2293224 I have not change anything with your function
this line pd.DataFrame(temp.apply(lambda x : normalized_Value(x['value'],x['RY']),axis=1).tolist() ,index = temp.index) returns the correct values. Problem comes in when assign back to variables

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.