mumbojumbo .Rolling().max().groupby()组合Python Pandas

发布于 2025-01-23 15:01:55 字数 828 浏览 5 评论 0原文

我希望做一个“滚动” .max().min().min()b列“分组”日期(A列A值)。但是,诀窍是它应该再次在每一行开始,所以我不能使用例如df ['max'] = df ['b']。滚动(10).max()。shift(-9)(Couse)我需要在小组结束的地方结束它 - 每个组都可以具有不同数量的行)或简单地分组一个列(因为我需要在每行启动时滚动最大最小的最小值,每个组都结束了 - 这意味着第1列C列C是B列中的最大行1-4,对于第2行C是B列的最大行2-4,对于第3列C是B列的最大第3-4行,对于第4列C列C是最大B列等第4行等。)。希望它使得c和d列是所需的结果。谢谢大家。

     A              B        C(max)   D(min)
1   2016-01-01      0        7        0     
2   2016-01-01      7        7        3
3   2016-01-01      3        4        3
4   2016-01-01      4        4        4  
5   2016-01-02      2        5        1
6   2016-01-02      5        5        1
7   2016-01-02      1        1        1         
8   2016-01-03      1        4        1
9   2016-01-03      3        4        2
10  2016-01-03      4        4        2
11  2016-01-03      2        2        2

I am looking to do a "rolling" .max() .min() of B column "groupedby" date(column A values). However, trick is it should start on every row again so i can not use for example anything like df['MAX'] = df['B'].rolling(10).max().shift(-9) (couse i need to end it where group ends - every group can have different number of rows) or simply groupby A column (becouse i need that rolling max min with start on each row and end where each group ends - which means for row 1 column C is max of rows 1-4 in column B, for row 2 column C is max of rows 2-4 from column B, for row 3 column C is max of rows 3-4 from column B, for row 4 column C is max of row 4 from column B etc etc..). Hope it make sence - columns C and D are desired results. Thank you all in advance.

     A              B        C(max)   D(min)
1   2016-01-01      0        7        0     
2   2016-01-01      7        7        3
3   2016-01-01      3        4        3
4   2016-01-01      4        4        4  
5   2016-01-02      2        5        1
6   2016-01-02      5        5        1
7   2016-01-02      1        1        1         
8   2016-01-03      1        4        1
9   2016-01-03      3        4        2
10  2016-01-03      4        4        2
11  2016-01-03      2        2        2

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

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

发布评论

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

评论(1

杀手六號 2025-01-30 15:01:55
df['C_max'] = df.groupby('A')['B'].transform(lambda x: x[::-1].cummax()[::-1])
df['D_min'] = df.groupby('A')['B'].transform(lambda x: x[::-1].cummin()[::-1])

            A  B  C(max)  D(min)  C_max  D_min
1   2016-01-01  0       7       0      7      0
2   2016-01-01  7       7       3      7      3
3   2016-01-01  3       4       3      4      3
4   2016-01-01  4       4       4      4      4
5   2016-01-02  2       5       1      5      1
6   2016-01-02  5       5       1      5      1
7   2016-01-02  1       1       1      1      1
8   2016-01-03  1       4       1      4      1
9   2016-01-03  3       4       2      4      2
10  2016-01-03  4       4       2      4      2
11  2016-01-03  2       2       2      2      2
df['C_max'] = df.groupby('A')['B'].transform(lambda x: x[::-1].cummax()[::-1])
df['D_min'] = df.groupby('A')['B'].transform(lambda x: x[::-1].cummin()[::-1])

            A  B  C(max)  D(min)  C_max  D_min
1   2016-01-01  0       7       0      7      0
2   2016-01-01  7       7       3      7      3
3   2016-01-01  3       4       3      4      3
4   2016-01-01  4       4       4      4      4
5   2016-01-02  2       5       1      5      1
6   2016-01-02  5       5       1      5      1
7   2016-01-02  1       1       1      1      1
8   2016-01-03  1       4       1      4      1
9   2016-01-03  3       4       2      4      2
10  2016-01-03  4       4       2      4      2
11  2016-01-03  2       2       2      2      2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文