title: pandas merge columns to create new column with comma separated values
body: <p>My dataframe has four columns with colors. I want to combine them into one column called &quot;Colors&quot; and use commas to separate the values.</p>
  <p>For example, I'm trying to combine into a Colors column like this :</p>
  <pre class="lang-none prettyprint-override"><code>ID  Black Red  Blue  Green  Colors   
  120 NaN   red  NaN   green  red, green  
  121 black Nan  blue  NaN    black, blue
  </code></pre>
  <p>My code is:</p>
  <pre class="lang-py prettyprint-override"><code>df['Colors'] = df[['Black, 'Red', 'Blue', 'Green']].apply(lambda x: ', '.join(x), axis=1)
  </code></pre>
  <p>But the output for ID 120 is:</p>
  <pre class="lang-none prettyprint-override"><code>, red, , green
  </code></pre>
  <p>And the output for ID 121 is:</p>
  <pre class="lang-none prettyprint-override"><code>black, , blue,
  </code></pre>
  <p>FOUND MY PROBLEM!
  Earlier in my code, I replaced <code>&quot;None&quot;</code> with <code>&quot; &quot;</code> instead of <code>NaN</code>. Upon making the change, plus incorporating feedback to insert <code>[x.notnull()]</code>, it works!</p>
  <pre class="lang-py prettyprint-override"><code>df['Black'].replace('None', np.nan, inplace=True)
  df['Colors'] = df[['Black, 'Red', 'Blue', 'Green']].apply(lambda x: ', '.join(x[x.notnull()]), axis=1)
  </code></pre>