English 中文(简体)
anda 或极地:发现比目前高的以前元素指数
原标题:pandas or Polars: find index of previous element larger than current one

附录一

data = {
     value : [1,9,6,7,3, 2,4,5,1,9]
}

每一行,我想找到比目前增加的最新内容的逐年增长。

因此,我的预期产出是:

[None, 0, 1, 2, 1, 1, 3, 4, 1, 0]
  • the first element 1 has no previous element, so I want None in the result
  • the next element 9 is at least as large than all its previous elements, so I want 0 in the result
  • the next element 6, has its previous element 9 which is larger than it. The distance between them is 1. So, I want 1 in the result here.

我知道,我可以在沙尔的 lo中这样做(如果我写延期的话,可在C/Rust)。

我的问题是:能否解决这一完全使用数据机操作? anda或极地,要么是罚款。 但只有数据基操作。

因此,以下人士无一可查:

  • apply
  • map_elements
  • map_rows
  • iter_rows
  • Python for loops which loop over the rows and extract elements one-by-one from the dataframes
问题回答

我猜测你正在寻找Rust执行算法的一部分,因此我提议如下:

import pandas as pd

data = {
     value : [1, 9, 6, 7, 3, 2, 4, 5, 1, 9]
}
df = pd.DataFrame(data)

values = df[ value ].tolist()

### Algorithm for implementation in Rust, C ...
r = []
for i in range(0, len(values)):
    curr = values[:i+1]
    prev = curr[:-1]
    last = curr[-1]
    if len(curr)>1:
        prev.reverse()
        dist=0
        for j in prev:
            if last < max(prev):
                dist+=1
            else:
                r.append(dist)
                break
            if last < j:
                r.append(dist)
                break
    else:
        r.append(None)

print(r)
[None, 0, 1, 2, 1, 1, 3, 4, 1, 0]

在Rust、Adhur或以下任何因素的计算之后,在数据框架内进行计算:

df[ out ] = r

print(df)
   value  out
0      1  NaN
1      9  0.0
2      6  1.0
3      7  2.0
4      3  1.0
5      2  1.0
6      4  3.0
7      5  4.0
8      1  1.0
9      9  0.0

This iterates only on the range of rows that this should look. It doesn t loop over the rows themselves in python. If your initial bound_range covers all the cases then it won t ever actually do a loop.

lb=0
bound_range=3
df=df.with_columns(z=pl.lit(None, dtype=pl.UInt64))
while True:
    df=df.with_columns(
        z=pl.when(pl.col( value )>=pl.col( value ).shift(1).cum_max())
            .then(pl.lit(0, dtype=pl.UInt64))
            .when(pl.col( z ).is_null())
            .then(
                pl.coalesce(
                    pl.when(pl.col( value )<pl.col( value ).shift(x))
                        .then(pl.lit(x, dtype=pl.UInt64))
                        for x in range(lb, lb+bound_range)
                )
            )
            .otherwise(pl.col( z ))
            )
    if df[1:][ z ].drop_nulls().shape[0]==df.shape[0]-1:
        break
    lb+=bound_range

例如,我制定了<条码>有条不紊的<>条/条码>至3条,以确保至少休息一次。 我带着1M随机随机分类的0至9(含括)进行处理,我定有约束——安排到50,并在2个秘密下进行。 你们可以通过检查检查,在休息室之间划出这一条码。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...