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!
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!
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