(1)apply和transform、groupby与聚合函数的Multi index
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:

groupby与聚合函数的Multi index
groupby与非聚合函数不会产生multi index,新的index与传入的Series或者DataFrame一致




groupby与单聚合函数,新的index会与groupby的keys一致


(可以通过reset_index(),然后left join到原来的DataFrame上)
groupby与滑动窗口、再加聚合函数,会产生Multi index,level=0是groupby的keys,level=1是原Series的index


浙公网安备 33010602011771号