What is the difference between pandas agg and apply function?

前端 未结 4 1118
闹比i
闹比i 2020-11-29 03:55

I can\'t figure out the difference between Pandas .aggregate and .apply functions.
Take the following as an example: I load a dataset, do a

4条回答
  •  醉梦人生
    2020-11-29 04:27

    The main difference between apply and aggregate is:

    apply()- 
        cannot be applied to multiple groups together 
        For apply() - We have to get_group()
        ERROR : -iris.groupby('Species').apply({'Sepal.Length':['min','max'],'Sepal.Width':['mean','min']})# It will throw error
        Work Fine:-iris.groupby('Species').get_group('Setosa').apply({'Sepal.Length':['min','max'],'Sepal.Width':['mean','min']})# It will throw error
            #because functions are applied to one data frame
    
    agg()- 
        can be applied to multiple groups together
        For apply() - We do not have to get_group() 
        iris.groupby('Species').agg({'Sepal.Length':['min','max'],'Sepal.Width':['mean','min']})
        iris.groupby('Species').get_group('versicolor').agg({'Sepal.Length':['min','max'],'Sepal.Width':['mean','min']})        
    

提交回复
热议问题