擅长:python、mysql、java
<p>您可以<code>groupby</code>重新格式化的<code>description</code>字段(无需修改<code>description</code>的原始内容),在该字段中,重新格式化是通过使用空格分割完成的,并通过使用<code>.str.split()</code>,<code>.str.join()</code>排除最后一部分。然后用<code>.agg()</code>进行聚合</p>
<p>通过使用<code>.round()</code>和<code>.astype()</code>四舍五入并转换为整数,进一步将输出重新格式化为所需的输出</p>
<pre><code>(df.groupby(
df['description'].str.split(' ').str[:-1].str.join(' ')
)
.agg({'total': 'sum', 'average': 'mean', 'number': 'sum'})
.round(0)
.astype(int)
).reset_index()
</code></pre>
<p>结果:</p>
<pre><code> description total average number
0 NFL football (blue) 207017 71 9992
1 NFL football (red) 164111 66 3746
2 NFL football (white) 292689 74 9971
</code></pre>