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(func, axis=0, raw=False, result_type=None, args=(), **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直接传入参数