Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a function as a parameter to another function?

I'm doing a bunch of operations on pandas dataframes. For example finding max, min and average inside columns and return the column names in a new column. Now I'm trying to wrap these things into a function, and use max() and/or min() as arguments in this function.

Below is a snippet that describes what I'm trying to do in a very simplified way. In its current state it also returns a description of the desired output. The snippet does not have the desired functionality and flexibility though.

The setup:

# Sample dataframe
df = pd.DataFrame({'col_A':[1,20,6,1,3]})

def findValue(function, df, colname):

    print(function) # just a placeholder
    df[colname] = df.max()[0]

    return df

df2 = findValue(function='max', df=df, colname='col_B')
print(df)

Output 1:

   col_A  col_B
0      1     20
1     20     20
2      6     20
3      1     20
4      3     20

A naive attempt:

# Sample dataframe
df = pd.DataFrame({'col_A':[1,20,6,1,3]})

# The function I would like to use in another function is max()

# My function 
def findValue(function, df, colname):
    df[colname] = df.function()[0]

    return df

df2 = findValue(function=max(), df=df , colname='col_B')
print(df)

Output 2:

Traceback (most recent call last):

  File "<ipython-input-7-85964ff29e69>", line 1, in <module>
    df2 = findValue(function=max(), df=df , colname='col_B')

TypeError: max expected 1 arguments, got 0

How can I change the above snippet so that I can change function = max() to function = min() or any other function in the arguments of findValue()? Or even define a list of functions to be used in a similar manner?

Thank you for any suggestions!

like image 845
vestland Avatar asked May 31 '26 07:05

vestland


1 Answers

You are very, very close. You pretty much just need to remove the parens when passing in the function. Here's a simplified example that loops over a list of function names, and appears to do what you want:

def findValue(func, x, y):
    return func(x, y)

for calc in (max, min):
    result = findValue(func=calc, x=1, y=10)
    print(result)

Output:

10
1
like image 151
stevieb Avatar answered Jun 04 '26 13:06

stevieb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!