title: Pandas data frame: replace nan with the mean of that row
body: <p>Assume I've the following data frame:</p>
  
  <pre><code>import numpy as np
  import pandas as pd
  df = pd.DataFrame({'A' : [np.nan, 1,2], 
                                    'B' : [3, np.nan, 4], 
                                    'C': [5,6,np.nan]}).T
  
  &gt;&gt;df
      0   1   2
  A NaN   1   2
  B   3 NaN   4
  C   5   6 NaN
  </code></pre>
  
  <p>To replace <code>np.nan</code> with the mean of the row in which the <code>np.nan</code> occurred, I used the <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.fillna.html" rel="nofollow">fillna</a> method as follows:</p>
  
  <pre><code>fill_value = pd.DataFrame({col: df.mean(axis=1) for col in df.columns})
  df.fillna(fill_value, inplace=True)
  
  Yields:
       0    1    2
  A  1.5  1.0  2.0
  B  3.0  3.5  4.0
  C  5.0  6.0  5.5
  </code></pre>
  
  <p>I am not sure why I can’t get <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.ma.MaskedArray.filled.html" rel="nofollow">filled</a> method to do the same thing:</p>
  
  <pre><code>mask = np.isnan(df)
  masked_df = np.ma.masked_array(df, mask)
  means = np.mean(masked_df, axis=0)
  df = masked_df.filled(means)
  
  Yields: 
  array([[ 4. ,  1. ,  2. ],
         [ 3. ,  3.5,  4. ],
         [ 5. ,  6. ,  3. ]])
  </code></pre>
  
  <p>Any idea how I can get <code>filled</code> method to also do the same thing? Thanks.</p>