pandas的groupby.apply和直接apply效果是不一样的

GroupBy.apply(func*args**kwargs)[source]

Apply function func group-wise and combine the results together.

The function passed to apply must take a dataframe as its first argument and return a DataFrame, Series or scalar. apply will then take care of combining the results back together into a single dataframe or series. apply is therefore a highly flexible grouping method.

While apply is a very flexible method, its downside is that using it can be quite a bit slower than using more specific methods like agg or transform. Pandas offers a wide range of method that will be much faster than using apply for their specific purposes, so try to use them before reaching for apply.

 

groupby的apply输入是一个df,输出也是一个df

 

DataFrame.apply(funcaxis=0raw=Falseresult_type=Noneargs=()**kwargs)[source]

Apply a function along an axis of the DataFrame.

Objects passed to the function are Series objects whose index is either the DataFrame’s index (axis=0) or the DataFrame’s columns (axis=1). By default (result_type=None), the final return type is inferred from the return type of the applied function. Otherwise, it depends on the result_type argument.

 

dataframe 的apply输入是一个元素。如果axis=1的话,那么输入就是一行的数据。

 

注意参数也是不一样的。dataframe的apply的args的是args=(),。而groupby的apply直接传入参数

posted @ 2022-02-24 15:02  dmesg  阅读(247)  评论(0编辑  收藏  举报