Pandas的apply和transform
Two major differences between the transform and apply groupby methods:
- apply implicitly passes all the columns for each group as a DataFrame to the custom function, while transform passes each columns for each group as a Series to the custom function.
- The custom function passed to apply can return a scalar, or a Series or DataFrame (or numpy array or even list). The custom function passed to transform must return a sequence(a one dimensional Series, array or list) the same length as the group.(返回与每组长度相同的序列)
transform每次处理一个Series(把group一列一列拆成Series),apply把整个组当成dataframe.
apply会进行聚合:

此时是Multindex

level=0:date,也就是分组键
level=1:index,是原DataFrame的索引


理解了apply的机制,就很容易去使用它,比如算两列相关系数,肯定是要在DataFrame上操作,就不会用transform:

如果想用transform来计算对数收益率,考虑到会传入每列作为Series,也就知道一开始就只能穿一列close:
