0

I want to pass a pandas method as an argument in a function. For example:

def myfunction(dataframe, method):
    return dataframe.method

I tried passing min() for example but that doesn't work. Thanks for your help!

1 Answers1

0

Use getattr:

def myfunction(dataframe, method):
    return getattr(dataframe, method)()

example:

myfunction(pd.DataFrame({'a': [1,2,3]}), 'min')

If you want the method and not the output of the method call:

def myfunction(dataframe, method):
    return getattr(dataframe, method)

example:

m = myfunction(pd.DataFrame({'a': [1,2,3]}), 'min')

# then call
m()

output:

a    1
dtype: int64
mozway
  • 194,879
  • 13
  • 39
  • 75