设置功能的默认值

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

我必须创建一个将数据框作为一个输入,将必需的列作为第二个输入的函数

def get_feature(df,cols=df.columns):
    . . .
    . . . 
    . . .
    return features

所以在这里我希望第二个参数是用户可以输入的列的列表,否则它应该将作为参数一个传递的数据框的所有列作为默认值,有什么办法吗?

python-3.x function parameters parameter-passing default
1个回答
0
投票

您可以做这样的事情:

def get_feature(df, cols=None):
    if cols is None:
        cols = df.columns
    . . . 
    . . .
    return features
© www.soinside.com 2019 - 2024. All rights reserved.