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还很陌生,真的很难找到什么方法来优雅地实现这些功能。我已经按照购物篮ID和日期对数据进行了排序,这样我就可以通过向前移动一行来批量获取上一次的销售记录。至于如何高效地计算平均销售额和最高销售额,我除了循环之外没有其他主意……有没有什么建议?
2 个回答
0
在编程中,有时候我们会遇到一些问题,比如代码运行不正常或者出现错误。这些问题可能是因为我们没有正确理解某些概念或者使用了不合适的代码。
当我们在网上寻找解决方案时,像StackOverflow这样的网站就非常有帮助。这里有很多开发者分享他们的经验和解决方案,帮助其他人解决类似的问题。
如果你在学习编程,遇到困难时,不妨去这些论坛看看,可能会找到你需要的答案。同时,也要记得多动手实践,只有通过实际操作,才能更好地理解编程的奥秘。
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()
4
这个方法应该能解决问题:
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()
你可以在这里了解更多关于分组和聚合的内容。