简体   繁体   中英

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

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 ie min will give you a Series in your case and the output of agg can either be a Series or a DataFrame

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM