title: Creating New Column In Pandas Dataframe Using Regex
body: <p>I have a column in a pandas df of type <code>object</code> that I want to parse to get the first number in the string, and create a new column containing that number as an <code>int</code>.</p>
  
  <p>For example:</p>
  
  <p>Existing df</p>
  
  <pre><code>    col
      'foo 12 bar 8'
      'bar 3 foo'
      'bar 32bar 98'
  </code></pre>
  
  <p>Desired df</p>
  
  <pre><code>    col               col1
      'foo 12 bar 8'    12
      'bar 3 foo'       3
      'bar 32bar 98'    32
  </code></pre>
  
  <p>I have code that works on any individual cell in the column series</p>
  
  <p><code>int(re.search(r'\d+', df.iloc[0]['col']).group())</code></p>
  
  <p>The above code works fine and returns 12 as it should. But when I try to create a new column using the whole series:</p>
  
  <p><code>df['col1'] = int(re.search(r'\d+', df['col']).group())</code></p>
  
  <p>I get the following Error:</p>
  
  <p>TypeError: expected string or bytes-like object</p>
  
  <p>I tried wrapping a <code>str()</code> around <code>df['col']</code> which got rid of the error but yielded all 0's in col1</p>
  
  <p>I've also tried converting <code>col</code> to a <code>list</code> of strings and iterating through the <code>list</code>, which only yields the same error. Does anyone know what I'm doing wrong? Help would be much appreciated.</p>