Adding a column in pandas df using a function
<p>I have a pandas df(see below). I want to add a column called "price" which I want to derive the values from using a function. How do I go about doing this?</p>

<pre><code>function:

    def getquotetoday(symbol):
        yahoo = Share(symbol)
        return yahoo.get_prev_close()


df:

Symbol    Bid      Ask
MSFT     10.25   11.15
AAPL     100.01  102.54


  (...)
</code></pre>

<p>In general, you can use the apply function. If your function is of only one column, you can use:</p>

<pre><code>df['price'] = df['Symbol'].apply(getquotetoday)
</code></pre>

<p>as @EdChum suggested. If your function happens to require multiple columns, you can use, for example, something like:</p>

<pre><code>df['new_column_name'] = df.apply(lambda x: my_function(x['value_1'], x['value_2']), axis=1)
</code></pre>

python function pandas dataframe yahoo