擅长:python、mysql、java
<p>根据你的命令:</p>
<pre><code>cols = ['prime', 'prime2']
df['changed'] = (df.groupby('id_police', sort=False, as_index=False)
.apply(lambda x: (x[cols].ne(0) & x[cols].shift(1).eq(0))
.any(axis=1).astype(int))
.reset_index(drop=True))
df
id_police date prime prime2 changed
0 p123 24/01/2017 0 0 0
1 p123 24/11/2017 0 30 1
2 p123 25/02/2018 10 10 1
3 b123 24/02/2018 20 20 0
4 b123 24/03/2018 30 0 0
</code></pre>
<p>使用<code>groupby</code>和<code>apply</code>对每个组应用函数。并设置<code>sort=False</code>,使其与主df的顺序相同。你知道吗</p>