Python时间序列对齐和“迄今”函数

2024-09-24 22:22:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个包含以下前三列的数据集。 包括购物篮ID(唯一标识符)、销售金额(美元)和交易日期。我想为数据集的每一行计算下面的列,我想用Python来计算。在

同一篮子商品的上一次销售(如有);当前篮子的销售数量到目前为止;当前篮子(如果有)的平均到目前为止当前篮子的最新销售(如果有)

Basket  Sale   Date       PrevSale SaleCount MeanToDate MaxToDate
88      $15 3/01/2012                1      
88      $30 11/02/2012      $15      2         $23        $30
88      $16 16/08/2012      $30      3         $20        $30
123     $90 18/06/2012               1      
477     $77 19/08/2012               1      
477     $57 11/12/2012      $77      2         $67        $77
566     $90 6/07/2012                1      

我对Python还很陌生,我真的很难找到任何一种奇特的方法来实现它。我已经按BasketID和Date对数据进行了排序(如上所述),因此我可以通过将每个篮子向前移动一个来批量获得以前的销售额。除了循环之外,不知道如何以有效的方式获取MeanToDate和MaxToDate。。。有什么想法吗?在


Tags: 数据id数量date交易标识符sale金额
2条回答
import pandas as pd
pd.__version__  # u'0.24.2'

from pandas import concat

def handler(grouped):
    se = grouped.set_index('Date')['Sale'].sort_index()
    return  concat(
        {
            'MeanToDate': se.expanding().mean(),   # cumulative mean
            'MaxToDate': se.expanding().max(),  # cumulative max
            'SaleCount': se.expanding().count(),   # cumulative count
            'Sale': se,                # simple copy
            'PrevSale': se.shift(1)   # previous sale
        },
        axis=1
     )

###########################
from datetime import datetime  
df = pd.DataFrame({'Basket':[88,88,88,123,477,477,566],
                  'Sale':[15,30,16,90,77,57,90],
                  'Date':[datetime.strptime(ds,'%d/%m/%Y') 
                          for ds in ['3/01/2012','11/02/2012','16/08/2012','18/06/2012',
                                    '19/08/2012','11/12/2012','6/07/2012']]})
#########

new_df = df.groupby('Basket').apply(handler).reset_index()

这应该可以做到:

from pandas import concat
from pandas.stats.moments import expanding_mean, expanding_count

def handler(grouped):
    se = grouped.set_index('Date')['Sale'].sort_index()
    # se is the (ordered) time series of sales restricted to a single basket
    # we can now create a dataframe by combining different metrics
    # pandas has a function for each of the ones you are interested in!
    return  concat(
        {
            'MeanToDate': expanding_mean(se), # cumulative mean
            'MaxToDate': se.cummax(),         # cumulative max
            'SaleCount': expanding_count(se), # cumulative count
            'Sale': se,                       # simple copy
            'PrevSale': se.shift(1)           # previous sale
        },
        axis=1
     )

# we then apply this handler to all the groups and pandas combines them
# back into a single dataframe indexed by (Basket, Date)
# we simply need to reset the index to get the shape you mention in your question
new_df = df.groupby('Basket').apply(handler).reset_index()

您可以阅读有关分组/聚合here的更多信息。在

相关问题 更多 >