如何根据极地条件计算当前行上方的行数?

kej*_*tos 5 python python-polars

让我们有一个极坐标 df:

df = pl.DataFrame(
    {
    'date': ['2022-01-01', '2022-01-02', '2022-01-07', '2022-01-17', '2022-03-02', '2022-06-05', '2022-06-07', '2022-07-02'],
    'col1': [4, 4, 2, 2, 2, 3, 2, 1],
    'col2': [1, 2, 3, 4, 1, 3, 3, 4],
    'col3': [2, 3, 4, 4, 3, 2, 2, 1]
    }
)
Run Code Online (Sandbox Code Playgroud)
日期 第 1 列 列2 第 3 列
2022-01-01 1 1 2
2022-01-02 1 2 3
2022-01-07 2 3 4
2022-01-17 2 4 1
2022-03-02 3 1 3
2022-06-05 3 2 2
2022-06-07 4 3 4
2022-07-02 4 4 1

df 按日期排序。我想创建一个列,它可以提供所有较早行(较低日期)的计数,其中所有 3 列的值都大于或等于当前行中的值。或者换句话说:

Count rows
    where row_index < current_row_index &
          col1[row_index] >= col1[current_row_index] &
          col2[row_index] >= col2[current_row_index] &
          col3[row_index] >= col3[current_row_index]
)
Run Code Online (Sandbox Code Playgroud)

结果应该是这样的:

日期 第 1 列 列2 第 3 列
2022-01-01 4 1 2 0
2022-01-02 4 2 3 0
2022-01-07 2 3 4 0
2022-01-17 2 4 4 0
2022-03-02 2 1 3 3
2022-06-05 3 3 2 0
2022-06-07 2 3 2 3
2022-07-02 1 4 1 1

我尝试过shiftqeoverwhen等的各种组合cum_count,但我一直无法弄清楚。我也找不到足够相似的问题来成功采用其答案。有没有办法使用 Polar 来实现这一目标?先感谢您。

jqu*_*ous 3

您还可以使用结构体将所有条件逻辑放入其中.cumulative_eval()

\n
cols = "col1", "col2", "col3"\n\ndf.with_columns(ge =\n   pl.struct(cols).cumulative_eval(\n       pl.all_horizontal(\n          pl.element().struct[col] >= pl.element().struct[col].last()\n          for col in cols\n       )\n       .sum() - 1 # subtract 1 as we compare each row against itself\n   )\n)\n
Run Code Online (Sandbox Code Playgroud)\n
shape: (8, 5)\n\xe2\x94\x8c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x90\n\xe2\x94\x82 date       \xe2\x94\x86 col1 \xe2\x94\x86 col2 \xe2\x94\x86 col3 \xe2\x94\x86 ge  \xe2\x94\x82\n\xe2\x94\x82 ---        \xe2\x94\x86 ---  \xe2\x94\x86 ---  \xe2\x94\x86 ---  \xe2\x94\x86 --- \xe2\x94\x82\n\xe2\x94\x82 date       \xe2\x94\x86 i64  \xe2\x94\x86 i64  \xe2\x94\x86 i64  \xe2\x94\x86 u32 \xe2\x94\x82\n\xe2\x95\x9e\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xaa\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xaa\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xaa\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xaa\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xa1\n\xe2\x94\x82 2022-01-01 \xe2\x94\x86 4    \xe2\x94\x86 1    \xe2\x94\x86 2    \xe2\x94\x86 0   \xe2\x94\x82\n\xe2\x94\x82 2022-01-02 \xe2\x94\x86 4    \xe2\x94\x86 2    \xe2\x94\x86 3    \xe2\x94\x86 0   \xe2\x94\x82\n\xe2\x94\x82 2022-01-07 \xe2\x94\x86 2    \xe2\x94\x86 3    \xe2\x94\x86 4    \xe2\x94\x86 0   \xe2\x94\x82\n\xe2\x94\x82 2022-01-17 \xe2\x94\x86 2    \xe2\x94\x86 4    \xe2\x94\x86 4    \xe2\x94\x86 0   \xe2\x94\x82\n\xe2\x94\x82 2022-03-02 \xe2\x94\x86 2    \xe2\x94\x86 1    \xe2\x94\x86 3    \xe2\x94\x86 3   \xe2\x94\x82\n\xe2\x94\x82 2022-06-05 \xe2\x94\x86 3    \xe2\x94\x86 3    \xe2\x94\x86 2    \xe2\x94\x86 0   \xe2\x94\x82\n\xe2\x94\x82 2022-06-07 \xe2\x94\x86 2    \xe2\x94\x86 3    \xe2\x94\x86 2    \xe2\x94\x86 3   \xe2\x94\x82\n\xe2\x94\x82 2022-07-02 \xe2\x94\x86 1    \xe2\x94\x86 4    \xe2\x94\x86 1    \xe2\x94\x86 1   \xe2\x94\x82\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xb4\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xb4\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xb4\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xb4\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x98\n
Run Code Online (Sandbox Code Playgroud)\n