擅长:python、mysql、java
<p>分组后,可以对<code>join</code>项执行<code>agg</code>函数</p>
<p>熊猫0.25+</p>
<pre><code>df = df.replace("Nan",np.NaN).ffill()
df.groupby('Client', sort=False)['Product'].agg(Product=('Product',','.join)).reset_index()
</code></pre>
<p>熊猫在0.25以下</p>
<pre><code>df=df.replace("Nan",np.NaN).ffill()
df.groupby('Client', sort=False)['Product'].agg([('Product', ','.join)]).reset_index()
</code></pre>
<p><strong>输出</strong></p>
<pre><code> Client Product
0 Alice A,B,C
1 Mallory B,C
2 Bob B
</code></pre>