<p><strong>更新答案</strong>:</p>
<p>多亏了这番评论,我才得以澄清问题,并想出了解决办法。在</p>
<p>要测试第一个需求(value>;=id+2的最小值),您需要一个系列来保存每个记录的id的最小值。如果您按id对dataframe进行分组并调用<code>value</code>系列的<code>min()</code>方法,这就是结果。您将需要从该系列中删除索引(<code>groupby()</code>方法会扰乱索引)。在</p>
<p>为了满足第二个要求(值出现在最小值的记录之后的记录中),只需使用上面确定的<code>idxmin()</code>方法。这将返回每个记录的id的最小值的一系列索引。在</p>
<p>然后,您可以将dataframe的<code>value</code>系列与第一个系列进行比较,并将dataframe的<code>index</code>与第二个序列进行比较,中间使用一个按位的<code>&</code>运算符,从而获得所需的结果(我认为)。在</p>
<pre><code>import pandas as pd
df = pd.DataFrame({'id': [1,1,1,1,1,1,1,2,2,2,2,2,2], 'value': [8,5,3,2,1,2,3,13,8,5,3,2,1]})
min_value = df.groupby('id')['value'].min()[df['id']]
min_value.reset_index(drop = True, inplace = True)
min_index = df.groupby('id')['value'].idxmin()[df['id']]
df['condition'] = (df['value'] >= min_value + 2) & (df.index > min_index)
</code></pre>
<p>结果是在dataframe中生成一个新的序列,它指示给定的记录是否满足所需的条件。在</p>
<p><strong>旧答案:</strong></p>
<p>这个问题仍然有点不清楚(你的理想输出是什么?),但我想到了这个代码,它测试每个id的分组值,以查看是否存在一个值,即最小值的索引之后有两个索引。在</p>
^{pr2}$
<p>有了这个,<code>test_id(1) == True</code>,<code>test_id(2) == False</code>。在</p>