【发布时间】:2020-08-20 21:49:45
【问题描述】:
所以我的数据框包含许多产品多年的销售详情,图表如下所示:
我正在尝试找出每种产品的销售窗口。
到目前为止我尝试过的:
我想到的方法是获取每年六个月间隔的最小、中值和最大日期值,并将(最小到中值)声明为最差销售期,将中值到最大声明为该产品的最佳销售窗口。我现在使用了六个月的代码,但也想在一年内获得它。哪个效果最好:
def dater(date):
print(date)
if type(date)==float:
return '-'
months = ['','Jan', 'Feb', 'Mar', 'Apr', 'May','Jun', 'Jul', 'Aug','Sep', 'Oct', 'Nov', 'Dec']
period = ['Start', 'Mid', 'End','End']
return months[date.month]+' '+period[date.day//10]
def grpRes(grp):
return pd.Series([grp.Date.min(), grp.Date.max(), grp.Amount.mean()],
index=['start', 'end', 'value'])
best_windows = pd.DataFrame(columns = df.select_dtypes(exclude='object').columns)
for col in df.select_dtypes(exclude='object').columns:
for year in ['2017', '2018', '2019', '2020']:
print(f'For year {year} and category {col}')
temp = df.loc[year,col][df[col]>=df[col].quantile(0.7)]
print('temp created')
if len(temp)>0:
du = temp.reset_index().rename(columns = {'order_start_date': 'Date', col:'Amount'})
res = du.groupby(du.Date.diff().dt.days.fillna(1, downcast='infer')
.gt(20).cumsum()).apply(grpRes)
res.index.name = 'chunk'
for row in res.iterrows():
print(row)
best_windows.loc[year+' Window: '+str(row[0]+1)+' start',col] = row[1].start.date().strftime('%d-%m-%Y')
然后,我根据所有年份的值将窗口定义为窗口的起始范围和结束范围。但似乎是一种可怕的方法。这虽然给了我不同年份的日期范围如下:
2017 Window: 1 end 2017 Window: 1 start 2017 Window: 2 end 2017 Window: 2 start 2018 Window: 1 end 2018 Window: 1 start 2018 Window: 2 end 2018 Window: 2 start 2018 Window: 3 end 2018 Window: 3 start 2019 Window: 1 end 2019 Window: 1 start 2019 Window: 2 end 2019 Window: 2 start 2019 Window: 3 end 2019 Window: 3 start 2020 Window: 1 end 2020 Window: 1 start 2020 Window: 2 end 2020 Window: 2 start 2020 Window: 3 end 2020 Window: 3 start 2020 Window: 4 end 2020 Window: 4 start
B 31-12-2019 08-11-2019 09-01-2020 01-01-2020 31-07-2020 11-02-2020
D 12-06-2017 13-05-2017 14-10-2017 16-08-2017 13-06-2018 24-05-2018 20-08-2018 11-07-2018 03-11-2018 27-09-2018 10-11-2019 22-10-2019 31-12-2019 28-12-2019 31-07-2020 01-01-2020
H 06-04-2018 23-03-2018 09-08-2018 27-06-2018 16-11-2018 02-11-2018 25-05-2019 21-04-2019 15-08-2019 12-07-2019 31-12-2019 30-10-2019 31-07-2020 01-01-2020
J 12-02-2017 15-01-2017 31-12-2017 25-10-2017 11-02-2018 01-01-2018 31-12-2018 12-10-2018 24-02-2019 01-01-2019 31-12-2019 10-10-2019 04-02-2020 01-01-2020
L 08-11-2018 03-11-2018 31-12-2018 06-12-2018 07-03-2019 01-01-2019 01-05-2019 24-04-2019 31-12-2019 02-09-2019 06-03-2020 01-01-2020 19-04-2020 10-04-2020 14-05-2020 10-05-2020 31-07-2020 26-07-2020
LO 31-12-2017 06-09-2017 03-01-2018 01-01-2018 31-12-2018 23-09-2018 10-02-2019 01-01-2019 31-12-2019 25-09-2019 11-02-2020 01-01-2020
M 11-09-2017 15-01-2017 15-10-2018 03-07-2018 02-05-2019 22-04-2019 24-11-2019 18-11-2019 13-05-2020 28-03-2020 23-07-2020 21-06-2020
P 03-05-2017 21-01-2017 19-10-2017 11-08-2017 23-04-2018 31-01-2018 10-10-2018 02-08-2018 23-04-2019 23-02-2019 06-10-2019 04-09-2019 04-04-2020 29-02-2020
S 26-07-2017 24-03-2017 01-07-2018 25-03-2018 01-05-2019 18-04-2019 10-08-2019 23-05-2019 31-07-2020 01-04-2020
SH 12-08-2017 07-05-2017 11-08-2018 05-05-2018 10-08-2019 01-05-2019 31-07-2020 29-04-2020
SK 31-12-2019 12-12-2019 01-01-2020 01-01-2020 31-07-2020 24-05-2020
SKO 26-09-2017 01-05-2017 19-09-2018 03-05-2018 25-07-2019 09-07-2019 31-07-2020 04-05-2020
SL 10-06-2017 24-05-2017 06-05-2018 06-05-2018 16-07-2018 31-05-2018 01-08-2019 12-03-2019 31-07-2020 16-02-2020
U 17-05-2019 18-04-2019 24-06-2019 10-06-2019 01-06-2020 27-03-2020 31-07-2020 25-06-2020
V 13-02-2017 15-01-2017 31-12-2017 14-09-2017 05-03-2018 01-01-2018 31-12-2018 25-09-2018 19-02-2019 01-01-2019 31-12-2019 22-10-2019 22-01-2020 01-01-2020
现在我可以使用我编写的日期函数将其转换为月份和精确的月份窗口:
best_windows = best_windows.transpose().applymap(dater)
但这给了我一个年度解决方案,而不是一个单一的销售窗口。
理想情况下我想要达到的目标:
一年中每种产品的最佳销售窗口和最差销售窗口,我可以说,嘿,在一年中的这个时候,这个产品很受欢迎(例如,产品 A 在 3 月底到 6 月中旬销售最好)大致由图中显示的销售百分比曲线的波峰/波谷,理想情况下还有过渡期,以便更好地了解每种产品的销售窗口。
数据样本:
我的数据如下所示。请注意,这些是基于每个类别所代表的总销售额的 %s。我说的百分比是指总销售额的百分比。假设总销售额为 10 美元。其中产品 A 售价 5 美元,B 售价 3 美元,C 售价 2 美元。然后 % 值为:A= 50%,B=30%,C=20%。当然,这仅在我尝试添加一整年数据的多个产品时才有效,因为它可以更好地解释我的数据中的季节性,而这在较小的样本中是无法检测到的。
【问题讨论】:
-
数据样本会很有帮助!
-
我添加了一个不错的大数据样本
-
@piRSquared 也许你可以帮忙。看到您对股票时间序列的出色回答,感觉您可能会有所帮助