<p>像这样:</p>
<pre><code>In [567]: df['NberrorsMatricule'] = df[~df.Priority.isin(['NC', 'EDC'])].\
...: groupby('Matricule')['Errorid']\
...: .transform('count')
</code></pre>
<p><strong>要删除<code>Nan</code>,请使用<code>ffill()</code>:</strong></p>
<pre><code>In [595]: df['NberrorsMatricule'] = df['NberrorsMatricule'].ffill()
In [596]: df
Out[596]:
Errorid Matricule Priority NberrorsMatricule
0 1 1 P1 2.0
1 2 1 P2 2.0
2 3 1 NC 2.0
3 4 2 P1 3.0
4 5 2 P4 3.0
5 6 2 EDC 3.0
6 7 2 P2 3.0
</code></pre>