title: Forming a sparse feature matrix data frame in Pandas
body: <p>I want to expand the 'features' column of this data frame so that I create a new data frame where these features become the column names.</p>
  
  <p>For example. From this,</p>
  
  <p><a href="https://i.stack.imgur.com/yY3Fz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yY3Fz.png" alt="Raw matrix"></a></p>
  
  <p>To this,</p>
  
  <p><a href="https://i.stack.imgur.com/m29sL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/m29sL.png" alt="Features matrix"></a></p>
  
  <p>My solution works but I don't think it is very good because there are lots of for-loops. Maybe there is a better approach thats takes advantage of features of the Pandas.DataFrame class?</p>
  
  <p>The code to generate the feature matrix is below,</p>
  
  <pre><code>def feature_data_frame_by_exploding_column(input_df, col_name):
  
      # Create data frame with same columns minus the column you want to explode
      df = input_df.copy()
      del df[col_name]
  
      # The items that you want to become new features
      all_new_features = []
      new_feature_list = input_df[col_name].values
      for ingred_list in new_feature_list:
          all_new_features.extend(ingred_list) # Extend vs append!
  
      # Add new features as columns of zeros
      for feature in all_new_features:
          df[feature] = 0
  
      # For each row in data frame set values that need to be 1
      for index in df.index:
          ingreds_arr = new_feature_list[index]
          df.loc[index, ingreds_arr] = 1
  
      return df
  
  df = pd.DataFrame(columns = ["id", "features"])
  df['id'] = [0,1]
  df['features'] = [["A", "B"], ["C", "D"]]
  df
  
  feature_data_frame_by_exploding_column(df,"features")
  </code></pre>