Python Pandas。用户定义函数缺少1个必要的位置参数

问题描述 投票:0回答:1

我定义了一个函数,它接受两个参数并返回两个值。它看起来像下面这样。

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

我在dataframe中调用了这个函数 在dataframe中创建两个新的列。我使用下面的代码来调用该函数。

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

然而,当我运行这段代码时,它抛出了一个缺少参数的错误信息。

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

我在代码中传递了两个参数,不知道为什么会抛出错误信息。谁能帮助纠正这个问题?

python python-3.x pandas
1个回答
1
投票

修改你的代码

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)
© www.soinside.com 2019 - 2024. All rights reserved.