Concatenating column values into row values in Pandas
<p>I have a dataframe like the below- both columns are strings, with the ValCol being a string of comma separated integers. The index is a generic integer index with no meaning.</p>

<pre><code>NameCol    ValCol
Name1      555, 333
Name2      433
Name1      999
Name3      123
Name2      533
</code></pre>

<p>What's the best way to aggregate it to</p>

<pre><code>NameCol    ValCol
Name1      555, 333, 999
Name2      433, 533
Name3      123
</code></pre>

<p>T don't care about the order of the comma separated integers, but I do need to keep commas between them. It likely will be a very small dataframe, &lt;100 records, so efficiency isn't critical.</p>

<p>I feel like there should be some groupby approach to this, but I haven't figured it out yet.</p>

<p>Using a <code>groupby</code> approach:</p>

<pre><code>df = df.groupby('NameCol')['ValCol'].apply(', '.join).reset_index()
</code></pre>

<p>The resulting output:</p>

<pre><code>  NameCol         ValCol
0   Name1  555, 333, 999
1   Name2       433, 533
2   Name3            123
</code></pre>

python pandas dataframe