salman
salman

Reputation: 316

Pandas Groupby with Aggregates

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()

Upvotes: 0

Views: 393

Answers (2)

FavorMylikes
FavorMylikes

Reputation: 1222

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

Upvotes: 0

Vishnudev Krishnadas
Vishnudev Krishnadas

Reputation: 10960

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

Upvotes: 1

Related Questions