标签: python-2.7 pandas
理想情况下,我希望它最终会像这样(注意modelIDs组合在一起,expand_mean,expanding_median函数,SaleCount等等):
我尝试对数据进行排序并通过'saledate'重新编制索引,然后使用'group_by'来执行每个modelID的函数,但似乎无法将其转换为所需的格式。我一直在研究看起来应该是相当简单的任务3天。任何建议都将不胜感激。
答案 0 :(得分:1)
尝试将transform和shift结合使用:
transform
shift
from pandas.stats.moments import * df.groupby(['ModelID', 'saledate']).transform(lambda x: expanding_mean(x.shift(1)))