Apply a map to DataFrame rows which are NaN
<p>I want to apply a mapping to a Pandas DataFrame, where some rows in one specific column are <code>NaN</code>. These are left overs from a prior mapping.</p>

<pre><code>mymap = defaultdict(str)
mymap["a"] = "-1 test"
mymap["b"] = "-2 test"
mymap["c"] = "-3 test"

df[ df["my_infos"].isnull() ] = df["something"].map(lambda ip: map_function(ip, mymap))
</code></pre>

<p>Here is the function:</p>

<pre><code>def map_function(ip, mymap):
    # do stuff
    for key, value in mymap.iteritems():
           # do stuff
           return stuff
    return other_stuff
</code></pre>

<p>This terminates after it iterates over the whole column, but shows:</p>

<blockquote>
  <p>File
  "/opt/anaconda2/lib/python2.7/site-packages/pandas/core/indexing.py",
  line 527, in _setitem_with_indexer
      raise ValueError('Must have equal len keys and value ' ValueError: Must have equal len keys and value when setting with an iterable</p>
</blockquote>

<p>Is this the correct way to select all rows in the <code>my_infos</code> column, which are NaN? I somehow sense... that's not the case.</p>

<p>I think you can select <code>NaN</code> values to both sides and then <code>map</code>:</p>

<pre><code>df[ df["my_infos"].isnull() ] = 
df.ix[ df["my_infos"].isnull(), "something"].map(lambda ip: map_function(ip, mymap))
</code></pre>

python pandas