如何使用有条件的窗口标准在Python中的不同索引级别上进行滚动总和

发布于 2025-01-24 09:26:33 字数 8005 浏览 0 评论 0原文

我想根据索引的不同级别进行滚动和滚动,但正在努力使其成为现实。而不是解释问题,而是在演示输入和所需的输出下以及正在寻找的见解。

因此,我有多个品牌,他们在以下分组的不同年度日的各种商品类别的销售中。我想要的是每天的动态滚动总和,按要求在一年中滚动窗户。

例如,如果有人问

emo问题1)直到某天(不包括那一天),他们对该特定品牌的特定类别的销售是什么。

我需要能够每天回答这个问题,即每一行应该具有一个数字,如表2.0所示。

我希望能够以一种方式进行编码,如果问题从2年变为3年,我只需要更改一个数字。 我还需要在当月的级别上做同样的事情。

demo问题2)直到某天(不包括那一天)他们对该特定类别的最后3个月出售是什么该特定品牌的那一年。

是演示输入

这些桌子由品牌,类别,年,月份,月,日和销售总和分组 所有信息和销售

表1.0

品牌类别年度每天每天销售
ABCBig Teppliances2021930
服装2021920
电子设备202010182
用户202010180
2021924
30
XYZBig Thepliances20124297
2013476
服装20124293
电子2013491
272
545
2015 20154277
52 22
粉丝20134144
4 540
20154 0 2015 4181 5
17 111111
20164 2016 41218
家具2012541
86
204
2013451
78
92
201541812
2715
524
173
音乐52012年4102013
20155616
4181018
20150
20164121
1613
UTENC2012582
20164163
182
201741213

以下是基于演示表的演示问题1的输出(最后2年不包括那天不包括那天)

表2.0

品牌类别的销售销售有条件0货物(直到最后2年)
ABC大型电器20219300
服装20219200
电子设备202010182年度
用户202010180 00
20219240
440 3 0 3
XYZBIG BIG电器201242970
20134767
服装201242930
电子20134910
2721
5 4 5453
201542778
5221
粉丝20134144 14 40 5 4 0
0442015
201541814
517115
20164121812
家具20125410
861
2047
201345111
7812
9220
20154181211
2717 1523
52438
17342
音乐界2012年455月18日100
2013061012
2015416106
18016
2016 2016411016
161611
USENCY20125824
201641630
1823
12 13 20172017 412135

END想法:

这个想法是基本上是在一年的专栏上进行滚动窗口,以维护2年跨度标准,并继续总结销售数字。

PS我确实需要一个快速解决方案,因此由于数据大小庞大,因此在启用了一个。函数的行,我认为这是不可行的。通过使用某种组滚动总和或支持列的更好解决方案确实很有帮助。

I want to do a rolling sum based on different levels of the index but am struggling to make it a reality. Instead of explaining the problem am giving below the demo input and desired output along with the kind of insights am looking for.

So I have multiple brands and each of their sales of various item categories in different year month day grouped by as below. What I want is a dynamic rolling sum at each day level, rolled over a window on Year as asked.

for eg, if someone asks

Demo question 1) Till a certain day(not including that day) what were their last 2 years' sales of that particular category for that particular brand.

I need to be able to answer this for every single day i.e every single row should have a number as shown in Table 2.0.

I want to be able to code in such a way that if the question changes from 2 years to 3 years I just need to change a number. I also need to do the same thing at the month's level.

demo question 2) Till a certain day(not including that day) what was their last 3 months' sale of that particular category for that particular year for that particular brand.

Below is demo input

The tables are grouped by brand,category,year,month,day and sum of sales from a master table which had all the info and sales at hour level each day

Table 1.0

BrandCategoryYearMonthDaySales
ABCBig Appliances2021930
Clothing2021920
Electronics202010182
Utensils202010180
2021924
30
XYZBig Appliances20124297
2013476
Clothing20124293
Electronics2013491
272
545
20154277
522
Fans20134144
540
20154181
51711
201641218
Furniture2012541
86
204
2013451
78
92
201541812
2715
524
173
Musical-inst201251810
2013456
201541610
180
20164121
1613
Utencils2012582
20164163
182
201741213

Below is desired output for demo question 1 based on the demo table(last 2 years cumsum not including that day)

Table 2.0

BrandCategoryYearMonthDaySalesConditional Cumsum(till last 2 years)
ABCBig Appliances20219300
Clothing20219200
Electronics2020101820
Utensils2020101800
20219240
304
XYZBig Appliances201242970
20134767
Clothing201242930
Electronics20134910
2721
5453
201542778
52215
Fans201341440
5404
201541814
517115
20164121812
Furniture20125410
861
2047
201345111
7812
9220
20154181211
271523
52438
17342
Musical-inst2012518100
201345610
2015416106
18016
2016412110
161311
Utencils20125820
201641630
1823
2017412135

End thoughts:

The idea is to basically do a rolling window over year column maintaining the 2 years span criteria and keep on summing the sales figures.

P.S I really need a fast solution due to the huge data size and therefore created a .apply function row-wise which I didn't find feasible. A better solution by using some kind of group rolling sum or supporting columns will be really helpful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

止于盛夏 2025-01-31 09:26:33

在这里,我为上述问题提供了示例解决方案。
我只是刻有ONR产品,以便解决方案是简单的

代码:

from datetime import date,timedelta
Input={"Utencils": [[2012,5,8,2],[2016,4,16,3],[2017,4,12,13]]}
Input1=Input["Utencils"]
Limit=timedelta(365*2)
cumsum=0
lis=[]
Tot=[]
for i in range(len(Input1)):
    if(lis):
        while(lis):
            idx=lis[0]
            Y,M,D=Input1[i][:3]
            reqDate=date(Y,M,D)-Limit
            Y,M,D=Input1[idx][:3]
            if(date(Y,M,D)<=reqDate):
                lis.pop(0)
                cumsum-=Input1[idx][3]
            else:
                break
    Tot.append(cumsum)
    lis.append(i)
    cumsum+=Input1[i][3]
print(Tot)

这里TOT将为给定数据输出所需的货物列。
Output:

[0, 0, 3]

Here you can specify the Time span using Number of days in Limit variable.
Hope this solves the problem you are looking for.

Here I'm giving a sample solution for the above problem.
I have concidered just onr product so that the solution would be simple

Code:

from datetime import date,timedelta
Input={"Utencils": [[2012,5,8,2],[2016,4,16,3],[2017,4,12,13]]}
Input1=Input["Utencils"]
Limit=timedelta(365*2)
cumsum=0
lis=[]
Tot=[]
for i in range(len(Input1)):
    if(lis):
        while(lis):
            idx=lis[0]
            Y,M,D=Input1[i][:3]
            reqDate=date(Y,M,D)-Limit
            Y,M,D=Input1[idx][:3]
            if(date(Y,M,D)<=reqDate):
                lis.pop(0)
                cumsum-=Input1[idx][3]
            else:
                break
    Tot.append(cumsum)
    lis.append(i)
    cumsum+=Input1[i][3]
print(Tot)

Here Tot would output the required cumsum column for the given data.
Output:

[0, 0, 3]

Here you can specify the Time span using Number of days in Limit variable.
Hope this solves the problem you are looking for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文