Home > Back-end >  Pandas Groupby with Aggregates
Pandas Groupby with Aggregates

Time:05-07

I am working with pandas and I was wondering if there is a difference based on which statistical functions are applied as shown in the below examples and if there are certain situations where one is preferred over another.

  1. df.groupby('A')['B'].agg('min')
  2. df.groupby('A')['B'].min()

CodePudding user response:

Both the code give the same desired output. But the agg function is more versatile in terms of the number of functions to apply.

For example, you can do the following with agg

df.groupby('A')['B'].agg(['min', 'max', 'mean'])

Another difference is that, applying a single function i.e. min will give you a Series in your case and the output of agg can either be a Series or a DataFrame

CodePudding user response:

In my experience, there is no difference between them, cause, the underlying function is np.min

def min(self, numeric_only: bool = False, min_count: int = -1):
    return self._agg_general(
        numeric_only=numeric_only, min_count=min_count, alias="min", npfunc=np.min
    )

npfunc=np.min

  • Related