Home > OS >  how to passing organized parameters in a simply way?
how to passing organized parameters in a simply way?

Time:10-22

Suppose I have a function with N parameters and Dataframe df with N columns.

def func(param1, param2, ... paramN)

I want to pass each columns as parameters.

func(df[0], df[1], ... df[N-1])

How to code it in a simple way if N is large.

CodePudding user response:

Try this:

def func(*args):
   param1 = args[0]
   ...

df = [1,2,3]
func(*df)

With *args you can pass a variable number of positional arguments.

CodePudding user response:

  • you can use * operator to do it

example:

def func(a,b,c):
    print(a,b,c)

func(*[1,2,3])

result:

1 2 3
  • Related