title: Pandas: Running a function to apply on a dataset
body: <p>I have been trying to figure out how to pass two variables (rows) to a function and get an output, but I have been having a lot of trouble with the grammar. </p>
  
  <p>I've been banging my head against a wall all day; here is what I looked at already:</p>
  
  <p>(I figured I was using apply wrong)
  <a href="https://stackoverflow.com/questions/32588797/pandas-how-to-apply-a-function-to-different-columns">Pandas: How to apply a function to different columns</a></p>
  
  <p><a href="https://stackoverflow.com/questions/19798153/difference-between-map-applymap-and-apply-methods-in-pandas/19798528#19798528">Difference between map, applymap and apply methods in Pandas</a></p>
  
  <p>I reread apply but it didn't help. I am working with the Titanic dataset (<a href="https://github.com/alexisperrier/packt-aml/blob/master/ch4/titanic.csv" rel="nofollow noreferrer">https://github.com/alexisperrier/packt-aml/blob/master/ch4/titanic.csv</a>)  and trying to replace empty ages with set numbers on the set. I tried two ways to do this:</p>
  
  <pre><code>Titanic.loc[(Titanic['pclass'] == 1) &amp; (Titanic['age'].isnull()), 'age'] = 35
  Titanic.loc[(Titanic['pclass'] == 2) &amp; (Titanic['age'].isnull()), 'age'] = 25
  Titanic.loc[(Titanic['pclass'] == 3) &amp; (Titanic['age'].isnull()), 'age'] = 20
  </code></pre>
  
  <p>(This code worked just fine, replacing empty 'ages' with predetermined values). My first attempt though was to create a function and apply it. Function:</p>
  
  <pre><code>def ClassAge(age,pclass):
      if age.isnull:
          if pclass == 1:
              n = 35
          if pclass == 2:
              n = 25
          if pclass == 3:
              n = 20
      return(n)
  </code></pre>
  
  <p>I tried to apply it using this:</p>
  
  <pre><code>Titanic.age.apply(ClassAge,Titanic['pclass'], axis=1)
  </code></pre>
  
  <p>Output:</p>
  
  <blockquote>
    <p>ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().</p>
  </blockquote>
  
  <p>Based on what I read in the other answers I tried this, because apply assumes that rows are the input.</p>
  
  <pre><code>Titanic[['age','pclass']].apply(ClassAge)
  </code></pre>
  
  <p>Which gave me this:</p>
  
  <blockquote>
    <p>TypeError: ("ClassAge() missing 1 required positional argument: 'pclass'", 'occurred at index age')</p>
  </blockquote>
  
  <p>As mentioned above I did resolve the issue using .loc, but just for educational purposes I'd like to understand what it is I am doing either in writing the function, or calling it (or both potentially).</p>