title: How to convert a dataframe to sparse matrix with mixed column types?
body: <p>I have a data frame of following format:</p>
  
  <pre><code>df:
  
  key   f1    f2
  k1    10    a, b, c
  k2    20    b, d
  k3    15    NaN
  </code></pre>
  
  <p>The column f2 has a bag of words as values. I want to convert this data frame into a sparse matrix, as distinct words in f2 run to a few thousands. The end result I am expecting is of following format:</p>
  
  <pre><code>key    f1  f2.a  f2.b  f2.c  f2.d
  k1     10   1     1     1     0
  k2     20   0     1     0     1
  k3     15   0     0     0     0
  </code></pre>
  
  <p>I could figure out how to independently create a sparse matrix just out of key and f2 field. I am first melting the column f2 so I get following dataframe:</p>
  
  <pre><code>df1:
  key  f2
  k1   a
  k1   b
  k1   c
  k2   b
  k2   d
  </code></pre>
  
  <p>Then I am encoding f2, and using the LabelEncoder from sklearn.preprocessing package to encode f2. Then I am creating a sparse matrix as follows:</p>
  
  <pre><code>df1['trainrow'] = np.arrange(df1.shape[0])
  sparse.csr_matrix((np.ones(df1.shape[0], (df1.trainrow, df1.f2_encoded)))
  </code></pre>
  
  <p>This creates a sparse matrix by doing a one-hot encoding of field f2. But I am not sure how I can concatenate this with the numerical field f1. </p>