title: How to fill na in pandas by the mode of a group
body: <p>I have a Pandas Dataframe like this:</p>
  <pre><code>  df = 
  
         a                    b
         a1                   b1
         a1                   b2
         a1                   b1
         a1                   Nan
         a2                   b1
         a2                   b2
         a2                   b2
         a2                   Nan
         a2                   b2
         a3                   Nan
  </code></pre>
  <p>For every value of <code>a</code>, <code>b</code> can have multiple values of <code>b</code> corresponding to it. I want to fill up all the <code>nan</code> values of <code>b</code> with the mode of <code>b</code> value grouped by the corresponding value of <code>a</code>.</p>
  <p>The resulting dataframe should look like the following:</p>
  <pre><code>  df = 
  
         a                    b
         a1                   b1
         a1                   b2
         a1                   b1
         a1                   ***b1***
         a2                   b1
         a2                   b2
         a2                   b2
         a2                   **b2**
         a2                   b2
         a3                   b2
  </code></pre>
  <p>Above <code>b1</code> was the mode of <code>b</code> corresponding to <code>a1</code>. Similarly, <code>b2</code> was the mode corresponding to <code>a2</code>. Finally, a3 had no data, so it fills it by global mode <code>b2</code>.</p>
  <p>For every nan value of column b, I want to fill it with the mode of the value of b column, but, for that particular value of a, whatever is the mode.</p>
  <p>EDIT:</p>
  <p>If there is a group <code>a</code> for which there is no data on <code>b</code>, then fill it by global mode.</p>