我想问问周围有没有人知道如何在极地进行滚动索引? 我个人try 过一些不适合我的解决方案(我将在下面展示它们):

What I'd like to do:Indexing the number of occurrences within the past X days by Name 示例:假设我想对过go 2天内发生的事件进行索引:

Name Date Counter
John 1 Jan 23 1
John 1 Jan 23 2
John 1 Jan 23 3
John 1 Jan 23 4
John 2 Jan 23 5
John 2 Jan 23 6
John 2 Jan 23 7
John 2 Jan 23 8
John 3 Jan 23 5
John 3 Jan 23 6
New Guy 1 Jan 23 1

在这种情况下,计数器从过go X天开始重置为"1"(例如,对于23年1月3日,从23年1月2日开始为"1"),或者如果检测到新名称

What I've tried:

df.groupby_rolling(index_column='Date', period='2d', by='Name', check_sorted=False).agg((pl.col("Date").rank(method='ordinal')).alias("Counter"))

上面不起作用,因为它输出:

Name Date Counter
John 1 Jan 23 1,2,3,4
John 1 Jan 23 1,2,3,4
John 1 Jan 23 1,2,3,4
John 1 Jan 23 1,2,3,4
John 2 Jan 23 1...8
John 2 Jan 23 1...8
John 2 Jan 23 1...8
John 2 Jan 23 1...8
John 3 Jan 23 1...6
John 3 Jan 23 1...6
New Guy 1 Jan 23 1
df.with_columns( Counter=pl.col("mask").rolling_sum(window_size='2d', by="Date") )

我创建了一个列"Mask",它只是一列"1",并试图将它们相加,但它输出:

Name Date Mask Counter
John 1 Jan 23 1 4
John 1 Jan 23 1 4
John 1 Jan 23 1 4
John 1 Jan 23 1 4
John 2 Jan 23 1 8
John 2 Jan 23 1 8
John 2 Jan 23 1 8
John 2 Jan 23 1 8
John 3 Jan 23 1 6
John 3 Jan 23 1 6

而且它也无法正确处理"New Guy",因为rolling_sum无法通过="Name","Date"来处理

df.with_columns(Counter = pl.col("Date").rank(method='ordinal').over(["Name", "Date"]) )

上述代码工作正常,但只能用于同一天内的索引(即period="1d")

附加注释:我也在Excel中做到了这一点,并且还使用了使用"for"-循环的brug/raw方法.两者都工作得很完美,但它们在处理大量数据时遇到了困难.

What I read: 答案中的一些帮助参考:(大多数都不起作用,因为它们有固定的滚动窗口,而不是"Date"的动态窗口)

How to implement rolling rank in Polars version 0.19

https://github.com/pola-rs/polars/issues/4808

How to do group_by_rolling grouped by day by hour in polars in Python?

How to groupby and rolling in polars?

https://docs.pola.rs/py-polars/html/reference/series/api/polars.Series.rank.html

https://docs.pola.rs/py-polars/html/reference/dataframe/api/polars.DataFrame.groupby_rolling.html

推荐答案

您可以从给出每个组的最大计数(在聚合内使用pl.len())的方法开始,并对Counter列进行后处理,以使其值在每个组内增加.

(
    df
    .rolling(index_column="Date", period="2d", group_by="Name", check_sorted=False)
    .agg(
        pl.len().alias("Counter")
    )
    .with_columns(
        (pl.col("Counter") - pl.len() + 1 + pl.int_range(pl.len())).over("Name", "Date")
    )
)
shape: (11, 3)
┌─────────┬────────────┬─────────┐
│ Name    ┆ Date       ┆ Counter │
│ ---     ┆ ---        ┆ ---     │
│ str     ┆ date       ┆ i64     │
╞═════════╪════════════╪═════════╡
│ John    ┆ 2023-01-01 ┆ 1       │
│ John    ┆ 2023-01-01 ┆ 2       │
│ John    ┆ 2023-01-01 ┆ 3       │
│ John    ┆ 2023-01-01 ┆ 4       │
│ John    ┆ 2023-01-02 ┆ 5       │
│ John    ┆ 2023-01-02 ┆ 6       │
│ John    ┆ 2023-01-02 ┆ 7       │
│ John    ┆ 2023-01-02 ┆ 8       │
│ John    ┆ 2023-01-03 ┆ 5       │
│ John    ┆ 2023-01-03 ┆ 6       │
│ New Guy ┆ 2023-01-01 ┆ 1       │
└─────────┴────────────┴─────────┘

Explanation.聚合后,Counter列将在每个长度为L的名称-日期-组内取常数值V.目标是让CounterV-L+1V的值(每行一个值).

因此我们可以

  1. Counter减go L-1
  2. 添加一个值从0到L-1增加的int范围.

Python相关问答推荐

这些变量是否相等,因为它们引用相同的实例,尽管它们看起来应该具有不同的值?

X射线扫描显示Docker中的pip漏洞,尽管图像中未安装pip

想要使用Polars groupby_Dynamic来缩减时间序列收件箱(包括空垃圾箱)

如何将不同长度的新列添加到现有的框架中

如何判断LazyFrame是否为空?

将列表中的元素替换为收件箱中的元素

如何修复使用turtle和tkinter制作的绘画应用程序的撤销功能

覆盖Django rest响应,仅返回PK

通过交换 node 对链接列表进行 Select 排序

剧作家Python:expect(locator).to_be_visible()vs locator.wait_for()

如何计算列表列行之间的公共元素

无法使用equals_html从网址获取全文

具有多个选项的计数_匹配

Pandas 滚动最接近的价值

PywinAuto在Windows 11上引发了Memory错误,但在Windows 10上未引发

我如何根据前一个连续数字改变一串数字?

使用Python更新字典中的值

当我try 在django中更新模型时,模型表单数据不可见

在Python中调用变量(特别是Tkinter)

将标签移动到matplotlib饼图中楔形块的开始处