title: How to append new columns to a pandas groupby object from a list of values
body: <p>I want to code a script that takes series values from a column, splits them into strings and makes a new column for each of the resulting strings (filled with NaN right now). As the df is <code>groupedby</code> Column1, I want to do this for every group</p>
  
  <p>My input data frame looks like this:</p>
  
  <pre><code>df1:
        Column1 Column2   
      0   L17      a,b,c,d,e
      1   L7       a,b,c
      2   L6       a,b,f
      3   L6       h,d,e
  </code></pre>
  
  <p>What I finally want to have is:</p>
  
  <pre><code>       Column1  Column2     a   b   c   d   e   f   h
      0   L17      a,b,c,d,e  nan nan nan nan nan nan nan
      1   L7       a,b,c      nan nan nan nan nan nan nan
      2   L6       a,b,f      nan nan nan nan nan nan nan
  </code></pre>
  
  <p>My code currently looks like this:</p>
  
  <pre><code>def NewCols(x):
      for item, frame in group['Column2'].iteritems():
          Genes = frame.split(',')
          for value in Genes:
              string = value
              x[string] = np.nan
              return x
  
  df1.groupby('Column1').apply(NewCols)
  </code></pre>
  
  <p>My thought behind this was that the code loops through Column2 of every grouped object, splitting the values contained in <code>frame</code> at comma and creating a list for that group. So far the code works fine. Then I added </p>
  
  <pre><code>for value in Genes:
     string = value
     x[string] = np.nan
     return x
  </code></pre>
  
  <p>with the intention of adding a new column for every value contained in the list <code>Genes</code>. However, my output looks like this:</p>
  
  <pre><code>   Column1  Column2    d
  0   L17      a,b,c,d,e nan
  1   L7       a,b,c     nan
  2   L6       a,b,f     nan
  3   L6       h,d,e     nan
  </code></pre>
  
  <p>and I am pretty much struck dumb. Can someone explain why only one column gets appended (which is not even named after the first value in the first list of the first group) and suggest how I could improve my code?</p>