擅长:python、mysql、java
<p>您可以执行几个<code>groupby</code>-<code>sum</code>,然后执行水平<code>concat</code>:</p>
<pre><code>>>> >>> pd.concat(
[df.quantity.groupby(df.buyer).sum()\
.reset_index().set_index('buyer').rename(columns={'quantity': 'totalBuy'}),
df.quantity.groupby(df.seller).sum()\
.reset_index().set_index('seller').rename(columns={'quantity': 'totalSell'})],
axis=1).fillna(0)
totalBuy totalSell
Frank 5 0
Jesse 0 4
John 14 5
Mark 0 10
</code></pre>