title: Getting a function to work on every row of a dataframe (pandas)
body: <p>I want to pass each cell of a column in a dataframe to a function which then creates a new cell </p>
  
  <p>I've looked <a href="https://stackoverflow.com/questions/19667927/pass-entire-contents-of-a-dataframe-to-a-function-in-pandas">here</a> and <a href="https://stackoverflow.com/questions/15118111/apply-function-to-each-row-of-pandas-dataframe-to-create-two-new-columns?rq=1">here</a> but these don't address my issue.</p>
  
  <p>I'm using an <a href="https://github.com/woodcrafty/PyETo" rel="nofollow noreferrer">obscure package</a> so I'll simplify the method using the base packages to ask the question, hopefully the issue will be clear.</p>
  
  <p><strong>Method:</strong></p>
  
  <p>Load the data</p>
  
  <pre><code>import pandas as pd
  import math
  
  df = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD'))
  </code></pre>
  
  <p>Pass the values of one column to a variable</p>
  
  <pre><code>lat = df['A']
  </code></pre>
  
  <p>Create a new column by applying the function to the variable</p>
  
  <pre><code>df['sol'] = df.apply(math.sqrt(lat))
  </code></pre>
  
  <p>This gives the error</p>
  
  <p><code>TypeError: cannot convert the series to &lt;type 'float'&gt;</code></p>
  
  <p>The error I'm getting using the pyeto package is actually </p>
  
  <pre><code>Traceback (most recent call last):
  
  File "&lt;ipython-input-10-b160408e9808&gt;", line 1, in &lt;module&gt;
  data['sol_dec'] = data['dayofyear'].apply(pyeto.sol_dec(data['dayofyear']), axis =1)            # Solar declination
  
  File "build\bdist.win-amd64\egg\pyeto\fao.py", line 580, in sol_dec
  _check_doy(day_of_year)
  
  File "build\bdist.win-amd64\egg\pyeto\_check.py", line 36, in check_doy
  if not 1 &lt;= doy &lt;= 366:
  
  File "C:\Users\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\core\generic.py", line 731, in __nonzero__
  .format(self.__class__.__name__))
  
  ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().`
  </code></pre>
  
  <p>I think the issue is the same in both cases, the function will not apply to every cell in the dataframe column, and produces an error. </p>
  
  <p>I want to be able to apply a function to each cell of a dataframe column (i.e. get the square root of each cell in column 'A'). Then store the result of this function as a variable (or another column in the dataframe i.e. have a 'sqrtA' column) , then apply a function to that variable (or column) and so on (i.e. have a new column which is 'sqrtA*100'.</p>
  
  <p>I can't figure out how to do this, and would really appreciate guidance. </p>
  
  <p><strong>EDIT</strong> </p>
  
  <p>@EdChum 's answer <code>df['A'].apply(math.sqrt)</code> or  <code>data['dayofyear'].apply(pyeto.sol_dec)</code> (for the package function) helped a lot.</p>
  
  <p>I'm now having issues with another function in the package which takes multiple arguments:</p>
  
  <pre><code>sha = pyeto.sunset_hour_angle(lat, sol_dec)
  </code></pre>
  
  <p>This function doesn't apply to a data-frame column, and I have <code>lat</code> and <code>sol_dec</code> stored as Series variables, but when I try to create a new column in the dataframe using these like so</p>
  
  <p><code>data['sha'] = pyeto.sunset_hour_angle(lat, sol_dec)</code> I get the same error as before...</p>
  
  <p>Attempting to apply the function to multiple columns: </p>
  
  <pre><code>data['sha'] = data[['lat'],['sol_dec']].apply(pyeto.sunset_hour_angle)
  </code></pre>
  
  <p>gives the error:</p>
  
  <pre><code>Traceback (most recent call last):
  
  File "&lt;ipython-input-28-7b603745af93&gt;", line 1, in &lt;module&gt;
  data['sha'] = data[['lat'],['sol_dec']].apply(pyeto.sunset_hour_angle)
  
  File "C:\Users\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\core\frame.py", line 1969, in __getitem__
  return self._getitem_column(key)
  
  File "C:\Users\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\core\frame.py", line 1976, in _getitem_column
  return self._get_item_cache(key)
  
  File "C:\Users\pflattery\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\core\generic.py", line 1089, in _get_item_cache
  res = cache.get(item)
  
  TypeError: unhashable type: 'list'
  </code></pre>