我有执行多项任务的功能。我需要传递可选字符串/变量和可选数据框以及其他值。例如,这是我的功能。def main(df,option=None,type=None, *args) if type == "cars": #multiple functions.. df = function1(df) results = function2(df) if option =="ex": results = function4(results) elif option =="CDS": result = function5(results) elif type == "buses": df2 = pd_read_csv("data2",sep="\t",header=0) # This is the optional data frame I wannna pass def func3(df2,results,df): result["col3"] =pd.merge(df2,df, on="col1") return result if option =="ex": results = function4(results) elif option =="CDS": result = function5(results) return(result)我已经将两个可选变量传递给我的函数main,option并且type. 现在我需要再传递一个可选变量,即df2. 但我不知道该怎么做。上面的例子是我的 main 函数中的一个模板。在这里,我想添加df2并在第一个循环 formain()中使用它。elifbuses非常感谢任何建议或帮助
2 回答

红糖糍粑
TA贡献1815条经验 获得超6个赞
您可以在 main 函数中使用 *args 参数来传递可选参数,例如 df2,如:
def main(df,option=None,type=None, *args):
df2=args[0] # here you will get the df2
# call main function as
main(df,option,type,df2)
或者
您还可以使用kwargs将关键字可选参数传递为,
def main(df,option=None,type=None, **kwargs):
df2=kwargs['df2'] # here you will get the df2
# call main function as
main(df,option,type,df2=df2)
希望这可以帮助!

jeck猫
TA贡献1909条经验 获得超7个赞
看起来你需要。
def main(df,option=None,type=None,df2=None, *args):
if df2 is not None:
#Process df2 ....
添加回答
举报
0/150
提交
取消