English 中文(简体)
找到符合面罩条件的第一行,并在符合条件的情况下选择一行
原标题:Finding the first row that meets conditions of a mask and selecting one row after it that meets a condition

详情见post

我的数据框架是:

import pandas as pd

df = pd.DataFrame(
    {
         a : [100, 1123, 123, 100, 1, 0, 1],
         b : [1000, 11123, 1123, 0, 55, 0, 1],
         c : [100, 1123, 123, 999, 11, 50, 1],
         d : [100, 1123, 123, 190, 1, 105, 1],
         e : [ a ,  b ,  c ,  d ,  e ,  f ,  g ],
    }
)

这是我想要的产出。 一栏:

      a      b     c     d  e   x
0   100   1000   100   100  a   NaN
1  1123  11123  1123  1123  b   NaN
2   123   1123   123   123  c   NaN
3   100      0   999   190  d   NaN
4     1     55    11     1  e   NaN
5     0      0    50   105  f   f
6     1      1     1     1  g   NaN

我的面罩是:

mask = (df.a > df.b)

这些是需要采取的步骤:

a) 选择符合面罩条件的第一行。

b) 以上步骤的<代码>a。

c) 第1行,上述数值为<代码>c和d。 相当于其中之一的是科索沃。

d) 检索<代码>e,并设立栏目

例如,上述数据框架:

a) 面罩第一行是<代码>3。

b) <代码>a的价值为100。

c) 从面罩后面的浏览量(4、5、......)中,100分在<条码>、<条/代码>和<条码>之间的第一行为第5行。 因此,f栏选入<代码>x。

d) 因此,f 选入<代码>x。

这一形象说明上述步骤:

“entergraph

这是我尝试的:

mask = (df.a > df.b)
val = df.loc[mask.cumsum().eq(1) & mask,  a ]

I prefer the solution to be generic like this answer.

如果你需要用其他微妙的不同条件测试该守则,我提供了一些额外的数据。 例如,如果没有符合面罩条件的一行。 在此情况下,<代码>x均为<代码>NaN。 栏目名称与以上编码相同。

df = pd.DataFrame({ a : [100, 1123, 123, -1, 1, 0, 1],  b : [1000, 11123, 1123, 0, 55, 0, 1], c : [100, 1123, 123, 999, 11, 50, 1],  d : [100, 1123, 123, 190, 1, 105, 1],  e : [ a ,  b ,  c ,  d ,  e ,  f ,  g ]})
df = pd.DataFrame({ a : [100, 1123, 123, 100, 1, 0, 1],  b : [1000, 11123, 1123, 0, 55, 0, 1],  c : [100, 1123, 123, 999, 11, -1, 1],  d : [100, 1123, 123, 190, 1, 10, 1],  e : [ a ,  b ,  c ,  d ,  e ,  f ,  g ]})
df = pd.DataFrame({ a : [100, 1123, 123, 1, 1, 0, 100],  b : [1000, 11123, 1123, 0, 55, 0, 1],  c : [100, 1123, 123, 999, 11, -1, 50],  d : [100, 1123, 123, 190, 1, 10, 101],  e : [ a ,  b ,  c ,  d ,  e ,  f ,  g ]})
df = pd.DataFrame({ a : [100, 1123, 123, 100, 1, 1000, 1], b : [1000, 11123, 1123, 0, 55, 0, 1], c : [100, 1123, 123, 999, 11, 50, 500],  d : [100, 1123, 123, 190, 1, 105, 2000],  e : [ a ,  b ,  c ,  d ,  e ,  f ,  g ]})
问题回答

我将为每一个步骤制定法典,最后,我将设法创建更多的<代码>generic。

首先,我将设立栏目<代码>x,附标值NaN

df[ x ] = np.NaN

Next I would use your mask with iloc[0] to get first matching row. And it I could get its name as index (because it is Series) and value [ a ] for next step

mask = (df.a > df.b)

first = df.loc[mask].iloc[0]
index = first.name
val   = first[ a ]  

接下来,我会利用这来制造面罩,以寻找下行。

mask = (df.index > index) & (val >= df[ c ]) & (val <= df[ d ])

row = df[mask].iloc[0]

print( row  : )
print(row)

我将使用本行的索引将<条码>e列入<条码>x的原始<条码>df。

df.loc[row.name,  x ] = row[ e ]

我没有检测到其他案件。

我不知道如何将其缩略到“密码”中。


正式工作法典:

import pandas as pd
import numpy as np

df = pd.DataFrame(
    {
         a : [100, 1123, 123, 100, 1, 0, 1],
         b : [1000, 11123, 1123, 0, 55, 0, 1],
         c : [100, 1123, 123, 999, 11, 50, 1],
         d : [100, 1123, 123, 190, 1, 105, 1],
         e : [ a ,  b ,  c ,  d ,  e ,  f ,  g ],
    }
)

df[ x ] = np.NaN

print(df)

mask = (df.a > df.b)

first = df.loc[mask].iloc[0]
index = first.name
val   = first[ a ]  
print( index: , index)
print( val  : , val)

mask = (df.index > index) & (val >= df[ c ]) & (val <= df[ d ])

row = df[mask].iloc[0]
print( row  : )
print(row)

df.loc[row.name,  x ] = row[ e ]
print(df)

http://www.un.org。

检查所有案件的法典。

其中一些带有空洞的面罩,因此我必须添加<>如果><>>>>>>,以核对(或try/<>,<<>>>>以发现错误。

import pandas as pd
import numpy as np

all_dfs = [
    pd.DataFrame({ a : [100, 1123, 123, 100, 1, 0, 1],
                   b : [1000, 11123, 1123, 0, 55, 0, 1],
                   c : [100, 1123, 123, 999, 11, 50, 1],
                   d : [100, 1123, 123, 190, 1, 105, 1],
                   e : [ a ,  b ,  c ,  d ,  e ,  f ,  g ]}),
    pd.DataFrame({ a : [100, 1123, 123, -1, 1, 0, 1], 
                   b : [1000, 11123, 1123, 0, 55, 0, 1], 
                   c : [100, 1123, 123, 999, 11, 50, 1], 
                   d : [100, 1123, 123, 190, 1, 105, 1], 
                   e : [ a ,  b ,  c ,  d ,  e ,  f ,  g ]}),
    pd.DataFrame({ a : [100, 1123, 123, 100, 1, 0, 1], 
                   b : [1000, 11123, 1123, 0, 55, 0, 1], 
                   c : [100, 1123, 123, 999, 11, -1, 1], 
                   d : [100, 1123, 123, 190, 1, 10, 1], 
                   e : [ a ,  b ,  c ,  d ,  e ,  f ,  g ]}),
    pd.DataFrame({ a : [100, 1123, 123, 1, 1, 0, 100], 
                   b : [1000, 11123, 1123, 0, 55, 0, 1], 
                   c : [100, 1123, 123, 999, 11, -1, 50], 
                   d : [100, 1123, 123, 190, 1, 10, 101], 
                   e : [ a ,  b ,  c ,  d ,  e ,  f ,  g ]}),
    pd.DataFrame({ a : [100, 1123, 123, 100, 1, 1000, 1],
                   b : [1000, 11123, 1123, 0, 55, 0, 1],
                   c : [100, 1123, 123, 999, 11, 50, 500], 
                   d : [100, 1123, 123, 190, 1, 105, 2000], 
                   e : [ a ,  b ,  c ,  d ,  e ,  f ,  g ]})
]

for df in all_dfs:
    print( --------------- )
    
    try:
        print( --- before --- )

        df[ x ] = np.NaN

        print(df)

        print( --- )

        mask = (df.a > df.b)

        if not mask.any():
            print("Can t find any matching row !!!")
            continue

        first = df.loc[mask].iloc[0]
        index = first.name
        val   = first[ a ]  
        print( index: , index)
        print( val  : , val)

        print( --- )

        mask = (df.index > index) & (val >= df[ c ]) & (val <= df[ d ])
        
        if not mask.any():
            print("Can t find any matching row !!!")
            continue
            
        row = df[mask].iloc[0]
        print( row  : )
        print(row)

        print( --- after --- )

        df.loc[row.name,  x ] = row[ e ]
        print(df)
    except Exception as ex:
        print(ex)

<><><>>>>

import numpy as np

target = d).loc[(d).a > d).b).cummax().cumsum().eq(1),  a ]
d)[ x ] = target
d)[ x ] = d)[ x ].ffill()
cond = d)[ x ].between(d)[ c ], d)[ d ]) & d)[ x ].notna()
d)[ x ] = np.where(cond.cummax().cumsum().eq(1), d)[ e ], float( nan ))

d)

“enterography


with with test test test test test test test test

d) = pd.DataFrame({ a : [100, 1123, 123, -1, 1, 0, 1],  b : [1000, 11123, 1123, 0, 55, 0, 1], c : [100, 1123, 123, 999, 11, 50, 1],  d : [100, 1123, 123, 190, 1, 105, 1],  e : [ a ,  b ,  c ,  d ,  e ,  f ,  g ]})

适用法典和回归:

“entergraph





相关问题
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 ]="...