xxyao
xxyao

Reputation: 549

how to passing organized parameters in a simply way?

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.

Upvotes: 0

Views: 38

Answers (2)

leaf_soba
leaf_soba

Reputation: 2518

  • 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

Upvotes: 1

David Miró
David Miró

Reputation: 2758

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.

Upvotes: 1

Related Questions