'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.
df.groupby('A')['B'].agg('min')df.groupby('A')['B'].min()
Solution 1:[1]
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
Solution 2:[2]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Vishnudev |
| Solution 2 | FavorMylikes |
