擅长:python、mysql、java
<p>我想你可以用<a href="https://docs.scipy.org/doc/numpy/reference/generated/numpy.where.html" rel="nofollow noreferrer">numpy.where</a>:</p>
<pre><code>result = df.assign(crime=np.where(df.crime.str.startswith('Burglary'), 'Burglary', df.crime))
print(result.groupby(['crime', 'year'], as_index=False).sum())
</code></pre>
<p><strong>输出</strong></p>
<pre><code> crime year count
0 Bank Robbery 2013 92
1 Burglary 2013 37165
2 Holdup / Robbery 2013 2907
</code></pre>
<p>注意<a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.assign.html" rel="nofollow noreferrer">assign</a>的使用,这样就可以保留原始的数据帧</p>