Key error in data-frame handling
<p>I have a dataframe <code>stockData</code>.  A part example looks like:</p>

<pre><code>Name: BBG.XCSE.CARLB.S_LAST_ADJ    BBG.XCSE.CARLB.S_FX  .....
date
2015-09-11    0.1340                           490.763
2015-09-14    0.1340                           484.263
2015-09-15    0.1340                           484.755
2015-09-16    0.1340                           507.703
2015-09-17    0.1340                           514.104  .....
</code></pre>

<p>each column has a data type , dtype: float64</p>

<p>I am looping a static data dataframe which contans every name in my universe  and I iterate through this, then iterating through each day for each name (in this example the name is BBG.XCSE.CARLB.S but there are hundreds of names in reality) taking the column 'name_LAST_ADJ' and multiplying by the column 'name_FX'.<br>
the code that I am using looks like:</p>

<pre><code>for i, row in staticData.iterrows():

        unique_id = i

        #Create new column for the current name that will take the result of the following calculation
        stockData[unique_id+"_LAST_ADJ_EUR"] = np.nan

        #Perform calculation - this is where I get the KeyError when there is no data in the name_ADJ_LAST column.
        stockData[unique_id+"_LAST_ADJ_EUR"] = stockData[unique_id+"_FX"]*stockData[unique_id+"_LAST_ADJ"]


    return stockData
</code></pre>

<p>However sometimes the data does not exist (because there is no history for the name) and I receive a key error because the columns for the name are not in the data-frame.</p>

<p>With the above code I am trying to create an additional column called name_LAST_ADJ_EUR and when there is data it should look like:</p>

<pre><code>Name: BBG.XCSE.CARLB.S_LAST_ADJ    BBG.XCSE.CARLB.S_FX     BBG.XCSE.CARLB.S_LAST_ADJ_EUR
    date
    2015-09-11    0.1340                       490.763              65.762242
    2015-09-14    0.1340                       484.263              64.891242
    2015-09-15    0.1340                       484.755              64.95717
    2015-09-16    0.1340                       507.703              68.032202
    2015-09-17    0.1340                       514.104              68.889936
</code></pre>

<p>and when there is data no data in the name_LAST_ADJ column is there a way generate an NaN output for he column so it looks like:</p>

<pre><code>Name:      BBG.XCSE.CARLB.S_LAST_ADJ_EUR
    date
    2015-09-11    NaN    
    2015-09-14    NaN       
    2015-09-15    NaN       
    2015-09-16    NaN         
    2015-09-17    NaN        
</code></pre>

<p>I have tried using the following:</p>

<pre><code>stockData[unique_id+"_LAST_ADJ_EUR"] = np.where((stockData[unique_id+"_LAST_ADJ"] == np.nan),stockData[unique_id+"_LAST_ADJ_EUR"]='NaN',stockData[unique_id+"_LAST_ADJ_EUR"] = stockData[unique_id+"_FX"] * stockData[unique_id+"_LAST_ADJ"])
</code></pre>

<p>which would be fine if there was a column but when there is no column to reference it throws the KeyError exception.</p>

<p>Any help much appreciated</p>

<p>In your <code>for</code> loop, try adding something akin to</p>

<pre><code>for uid, row in staticData.iterrows():
    if uid not in stockData.columns:
        stockData[uid + "_FX"] = np.nan
        stockData[uid + "_LAST_ADJ"] = np.nan

    # continue with what you have:
    # no longer needed
    #stockData[uid+"_LAST_ADJ_EUR"] = np.nan

    stockData[uid+"_LAST_ADJ_EUR"] = stockData[uid+"_FX"]*stockData[uid+"_LAST_ADJ"]
</code></pre>

<p>While doing it inside the <code>for</code> loop is probably most efficient, you could also do it all at once like:</p>

<pre><code>stockData = pd.concat([stockData, pd.DataFrame(columns=staticData.index)])
</code></pre>

<p>For example:</p>

<pre><code>df = pd.DataFrame(np.random.rand(10, 3), columns=list('abc'))
          a         b         c
0  0.627303  0.183463  0.714470
1  0.458124  0.135907  0.515340
2  0.629373  0.725247  0.306275
3  0.113927  0.259965  0.996407
4  0.321131  0.734002  0.766044
5  0.740858  0.238741  0.531810
6  0.063990  0.974056  0.178260
7  0.977651  0.047287  0.435681
8  0.972060  0.606288  0.600896
9  0.250377  0.807237  0.153419

pd.concat([df, pd.DataFrame(columns=list('abcde'))])
          a         b         c    d    e
0  0.627303  0.183463  0.714470  NaN  NaN
1  0.458124  0.135907  0.515340  NaN  NaN
2  0.629373  0.725247  0.306275  NaN  NaN
3  0.113927  0.259965  0.996407  NaN  NaN
4  0.321131  0.734002  0.766044  NaN  NaN
5  0.740858  0.238741  0.531810  NaN  NaN
6  0.063990  0.974056  0.178260  NaN  NaN
7  0.977651  0.047287  0.435681  NaN  NaN
8  0.972060  0.606288  0.600896  NaN  NaN
9  0.250377  0.807237  0.153419  NaN  NaN
</code></pre>

python pandas