<p>我有一个熊猫数据框df:</p>
<pre><code>+------+---------+
| team | user |
+------+---------+
| A | elmer |
| A | daffy |
| A | bugs |
| B | dawg |
| A | foghorn |
| B | speedy |
| A | goofy |
| A | marvin |
| B | pepe |
| C | petunia |
| C | porky |
+------+---------
</code></pre>
<p>我想找到或编写一个函数来返回一个数据帧,我将使用以下命令在MySQL中返回该数据帧:</p>
<pre><code>SELECT
team,
GROUP_CONCAT(user)
FROM
df
GROUP BY
team
</code></pre>
<p>对于以下结果:</p>
<pre><code>+------+---------------------------------------+
| team | group_concat(user) |
+------+---------------------------------------+
| A | elmer,daffy,bugs,foghorn,goofy,marvin |
| B | dawg,speedy,pepe |
| C | petunia,porky |
+------+---------------------------------------+
</code></pre>
<p>我可以通过遍历行并添加到字典中来想出令人讨厌的方法,但必须有更好的方法。</p>