title: Prevent leading and trailing commas using group() & agg() statements in a large DataFrame, Pandas
body: <p>I have a large DataFrame I'm working where I need to groupby() by one of the ID columns and place everything else in a csv list.  I've got one approach that also utlizes the agg() method that works well.
  However, I'm noticing in the case where I have an empty (null) value in a cell, this approach will preserve the empty (null) with a leading or trailing commas.  I need to prevent that.</p>
  <p>Replacing each column one at a time with either .lstrip(',') or .rstrip(',') is probably out of the question due to the nature of the DataFrame.  I also need to preserve the empty (null) in the source data, so I can't go back and auto fill in a dummy value.</p>
  <p>What it looks like...</p>
  <p><a href="https://i.stack.imgur.com/1zgD3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1zgD3.png" alt="enter image description here" /></a></p>
  <p>What I need...</p>
  <p><a href="https://i.stack.imgur.com/Ke74b.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ke74b.png" alt="enter image description here" /></a></p>
  <pre><code># Needed Libraries
  import numpy as np
  import pandas as pd
  
  # Sample Dataset
  columnsNames = [&quot;IDColumn&quot;, &quot;ColB&quot;, &quot;ColC&quot;]
  valuesList = [[&quot;a&quot;, &quot;b&quot;, &quot;c&quot;], [&quot;a&quot;, &quot;&quot;, &quot;c&quot;], [&quot;a&quot;, &quot;b&quot;, &quot;&quot;]]
  df = pd.DataFrame(columns=columnsNames, data=valuesList)
  
  # groupby() &amp; agg() statement 
  df = df.groupby(['IDColumn']).agg(lambda x: ','.join([str(elem) for elem in (list(set(x)))])).replace(np.nan, &quot;&quot;).reset_index()
  df
  </code></pre>
  <p>Thx.</p>