3

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!

2
  • 1
    Not that this is exactly what you are looking for, but are you aware of the describe() method? Will return a DataFrame of max, min, mean, std. dev., count for each col in the DataFrame (or a Series of these values, if run on a Series). [docs] (pandas.pydata.org/pandas-docs/stable/generated/…) Commented Jun 22, 2017 at 14:41
  • 1
    If you have a look at the link in the question, you'll see that I'm going to use what I was looking for as part of a bigger function where I'm not only using the measures covered by describe(). But it will certainly be useful, so thank you for the tip! Commented Jun 23, 2017 at 6:41

1 Answer 1

2

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 
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! This is exactly what I'm looking for. Does this technique have a name? I'm asking because I don't think 'change functions within a python function' is the best way to describe what we're doing here. Also, I did not have much success googling this.
@vestland Essentially, you're passing a function as a parameter to another function.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.