0
import pandas as pd

def func_sum(df, cols, col):
    res = df[cols].groupby(col).sum()
    return res

def func_count(df, cols,col):
    res = df[cols].groupby(col).count()
    return res

def func_ave(df, cols, col):
    res = df[cols].groupby(col).mean()
    return res

The way I did to combine these three functions is like below, which is not very elegant.

def func(df, cols, col, method):
    if method == 'sum':
        return df[cols].groupby(col).sum()
    if method == 'count':
        return df[cols].groupby(col).count()
    if method == 'mean':
        return df[cols].groupby(col).mean()

I wonder if there is a better way to do this without using IF ELSE statement. How Can I Pass the function of (sum, count, or mean) as a variable and then let's the passed function variable to be called inside the main 'func' function.

I would greatly appreciate any suggestions.

glibdud
  • 7,550
  • 4
  • 27
  • 37
Elsa Li
  • 673
  • 3
  • 9
  • 19
  • Possible duplicate of [Calling a function of a module by using its name (a string)](https://stackoverflow.com/questions/3061/calling-a-function-of-a-module-by-using-its-name-a-string) – iacob Apr 25 '19 at 13:25

1 Answers1

1

Use groupby.agg, which takes one or more functions by reference or by name.

For example:

def func(df, cols, col, method):
    return df[cols].groupby(col).agg(method)

func(df, cols, col, pd.Series.sum)
func(df, cols, col, 'count')
func(df, cols, col, np.mean)
0x5453
  • 12,753
  • 1
  • 32
  • 61