今天被pandas.DataFrame.apply和pandas.core.groupby.GroupBy.apply搞糊涂了,在搜索的过程中发现很多人跟我一样弄糊涂了。
这是
DataFrame.apply
Applies function along input axis of DataFrame.
Objects passed to functions are Series objects having index
either the DataFrame's index (axis=0) or the columns (axis=1).
Return type depends on whether passed function aggregates, or the
reduce argument if the DataFrame is empty.
这是pandas.core.groupby.GroupBy.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, a series or a 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. 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.
很明显这两完全不是一回事,一个是将Series传给函数,一个是dataframe。
还有人说DataFrame.apply “既可以作用于一行或者一列的元素,也可以作用于单个元素。”,从结果上来看这个说法有时是对的,但作用于单个元素的实质是Series具有numpy广播的特性,并不是真的像applymap一样作用在每个元素上。