title: How to write a return value of a function into new column of a pandas dataframe
body: <p>I have a pandas dataframe containing a column with strings (that are comma separated substrings). I want to remove some of the substrings and write the remaining ones to a new column in the same dataframe. </p>
  
  <p>The code I have written to do this looks like this:</p>
  
  <pre><code>def remove_betas(df):
      for index,row in df.iterrows():
          list= row['Column'].split(',')
          if 'substring' in list:
              list.remove('beta-lactam')
              New= (',').join(list)
          elif not 'substring' in list:
              New= (',').join(Gene_list)
      return New
      df['NewColumn'].iloc[index]=New
  
  
  
  
  
  
   df.apply(remove_betas, axis=1)
  </code></pre>
  
  <p>When I run it, my new column contains only zeros. The thought behind this code is to get each string for each row in df, split it at comma into substrings and search the resulting list for the substring I want to remove. After removal, I join the list back together into a string and write that to a new column of df, at the same index position as the corresponding row. </p>
  
  <p>What do I have to change to write the resulting substrings to a new column in the desired manner?</p>
  
  <p><strong><em>EDIT</em></strong></p>
  
  <p>By the way, I have tried to write a lambda expression as in <a href="https://stackoverflow.com/questions/18472634/how-to-compute-a-new-column-based-on-the-values-of-other-columns-in-pandas-pyt">how to compute a new column based on the values of other columns in pandas - python</a> , but I cannot really figure out how to do everything in a vectorized function. </p>
  
  <p>I also tried replacing the substring with nothing ( as in <code>df.column.replace('x,?', '')</code>, but that does not work since I have to count the lists later. Therefore the substring must be removed as in <code>list.remove('substring')</code></p>