<p>用途:</p>
<pre><code>m = df['Hour'].eq(12) & df['Min'].eq(0)
df['Result'] = df['Bool'].shift(-1).groupby(m.cumsum()).transform('any') | df['Bool']
</code></pre>
<p>解释:</p>
<ol>
<li>使用<code>==</code>的<a href="http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.eq.html" rel="nofollow noreferrer">^{<cd2>}</a>创建布尔掩码</li>
<li>然后将<a href="http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.shift.html" rel="nofollow noreferrer">^{<cd4>}</a>与<code>-1</code>一起使用一行</li>
<li>按<a href="http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.cumsum.html" rel="nofollow noreferrer">^{<cd6>}</a>创建组</li>
<li>使用<a href="http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.core.groupby.GroupBy.transform.html" rel="nofollow noreferrer">^{<cd7>}</a>和<a href="http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.core.groupby.GroupBy.any.html" rel="nofollow noreferrer">^{<cd8>}</a>进行测试<code>True</code>每组<code>g</code></li>
<li>最后一个带有<code>|</code>表示位或的布尔列</li>
</ol>
<hr/>
<pre><code>m = df['Hour'].eq(12) & df['Min'].eq(0)
print (df.assign(m = df['Hour'].eq(12) & df['Min'].eq(0),
shift = df['Bool'].shift(-1),
g = m.cumsum(),
transform = df['Bool'].shift(-1).groupby(m.cumsum()).transform('any'),
Result=df['Bool'].shift(-1).groupby(m.cumsum()).transform('any')|df['Bool']))
Bool Hour Min m shift g transform Result
0 False 12 0 True False 1 False False
1 False 12 30 False False 1 False False
2 False 24 0 False False 1 False False
3 False 24 30 False False 1 False False
4 False 12 0 True False 2 True True
5 False 12 30 False False 2 True True
6 False 24 0 False False 2 True True
7 False 24 30 False True 2 True True
8 True 12 0 True False 3 False True
9 False 12 30 False False 3 False False
10 False 24 0 False False 3 False False
11 False 24 30 False False 3 False False
12 False 12 0 True False 4 False False
13 False 12 30 False False 4 False False
14 False 24 0 False False 4 False False
15 False 24 30 False False 4 False False
16 False 12 0 True False 5 False False
17 False 12 30 False False 5 False False
18 False 24 0 False False 5 False False
19 False 24 30 False False 5 False False
20 False 12 0 True False 6 True True
21 False 12 30 False False 6 True True
22 False 24 0 False True 6 True True
23 True 24 30 False NaN 6 True True
</code></pre>
<hr/>
<p>为@Wen添加新条件答案:</p>
<pre><code>m = (~df.Bool&df.Hour.eq(12)&df.Min.eq(0))
s=m.iloc[::-1].groupby(df.Bool.iloc[::-1].cumsum()).transform('idxmax')
df['result']=df.index>=s.iloc[::-1]
</code></pre>
<p>也给匡宏这样的回答:</p>
<pre><code>s = df['Bool'].shift(-1)
m = df['Hour'].eq(12) & df['Min'].eq(0)
df['Result'] = df['Bool'] | s.where(s).groupby(m.cumsum()).bfill()
</code></pre>