I have a function which goes something like:
import pandas as pd
import numpy as np
def my_fun(df, calc_type):
if calc_type == 'sum':
return df['col_name'].sum()
elif calc_type == 'mean':
return df['col_name'].mean()
elif calc_type == 'std':
return df['col_name'].std()
# sample df
df = pd.DataFrame({'col_name': np.random.normal(25, 3.5, 1000)})
# function call
my_func(df, 'sum')
I was wondering if there's a more elegant, one liner, way to do so.
There is getattr
which would be great if it worked on methods. Is there something similar?