title: Is there a better way to convert 'object' type array to numpy array by replacing 'na' with mean?
body: <p>I have an array of strings with some elements such as 'na' that can't be converted to float by using <code>x.astype(np.float)</code> as given <a href="https://stackoverflow.com/a/3877247/6701627">here</a>.</p>
  
  <p>Please suggest any better way than the way I did it. Please find the procedure below (it is a snippet from my jupyter notebook, I have shown the intermediate steps just to demonstrate the changes):</p>
  
  <p>In [4]: <code>val_inc</code></p>
  
  <p>Out [4]: </p>
  
  <pre><code>array(['na', '38.012', '38.7816', '38.0736', '40.7118', '44.7382',
         '39.6416', '38.9177', '36.9031', 43.2611, '38.2732', 40.7129,
         '37.2844', '39.5835', 43.9194, '42.5485', '36.9052', 'na', 41.9264,
         45.3568, '44.6239', 38.1079, 45.2393, '32.785', '44.6239',
         '38.0216', '38.4608', '42.5644', '35.3127', 33.2936, '33.0556',
         '40.4476', 35.6581, '35.5574', '43.1096', '34.4751', 42.0554,
         40.3944, '40.2466', '32.2567', 'na', '38.8594', '43.947', 41.7973,
         '41.8105', 40.3797, 31.2868, '45.3644', '40.7177', '41.8558',
         '38.9249', '33.2077', '42.4053', '42.559'], dtype=object)
  </code></pre>
  
  <p>In [5]: <code>val_inc[val_inc == 'na']='0'</code></p>
  
  <p>In [6]: <code>val_inc</code></p>
  
  <p>Out [6]: </p>
  
  <pre><code>array(['0', '38.012', '38.7816', '38.0736', '40.7118', '44.7382',
         '39.6416', '38.9177', '36.9031', 43.2611, '38.2732', 40.7129,
         '37.2844', '39.5835', 43.9194, '42.5485', '36.9052', '0', 41.9264,
         45.3568, '44.6239', 38.1079, 45.2393, '32.785', '44.6239',
         '38.0216', '38.4608', '42.5644', '35.3127', 33.2936, '33.0556',
         '40.4476', 35.6581, '35.5574', '43.1096', '34.4751', 42.0554,
         40.3944, '40.2466', '32.2567', '0', '38.8594', '43.947', 41.7973,
         '41.8105', 40.3797, 31.2868, '45.3644', '40.7177', '41.8558',
         '38.9249', '33.2077', '42.4053', '42.559'], dtype=object)
  </code></pre>
  
  <p>In [7]: <code>val_inc = val_inc.astype(np.float)</code></p>
  
  <p>In [8]: <code>val_inc</code></p>
  
  <p>Out [8]: </p>
  
  <pre><code>array([  0.    ,  38.012 ,  38.7816,  38.0736,  40.7118,  44.7382,
          39.6416,  38.9177,  36.9031,  43.2611,  38.2732,  40.7129,
          37.2844,  39.5835,  43.9194,  42.5485,  36.9052,   0.    ,
          41.9264,  45.3568,  44.6239,  38.1079,  45.2393,  32.785 ,
          44.6239,  38.0216,  38.4608,  42.5644,  35.3127,  33.2936,
          33.0556,  40.4476,  35.6581,  35.5574,  43.1096,  34.4751,
          42.0554,  40.3944,  40.2466,  32.2567,   0.    ,  38.8594,
          43.947 ,  41.7973,  41.8105,  40.3797,  31.2868,  45.3644,
          40.7177,  41.8558,  38.9249,  33.2077,  42.4053,  42.559 ])
  </code></pre>
  
  <p>In [9]: <code>np.mean(val_inc[val_inc!=0.])</code></p>
  
  <p>Out [9]: <code>39.587374509803915</code></p>
  
  <p>In [10]: <code>val_inc[val_inc==0.]=np.mean(val_inc[val_inc!=0.])</code></p>
  
  <p>In [11]: <code>val_inc</code></p>
  
  <p>Out [11]: </p>
  
  <pre><code>array([ 39.58737451,  38.012     ,  38.7816    ,  38.0736    ,
          40.7118    ,  44.7382    ,  39.6416    ,  38.9177    ,
          36.9031    ,  43.2611    ,  38.2732    ,  40.7129    ,
          37.2844    ,  39.5835    ,  43.9194    ,  42.5485    ,
          36.9052    ,  39.58737451,  41.9264    ,  45.3568    ,
          44.6239    ,  38.1079    ,  45.2393    ,  32.785     ,
          44.6239    ,  38.0216    ,  38.4608    ,  42.5644    ,
          35.3127    ,  33.2936    ,  33.0556    ,  40.4476    ,
          35.6581    ,  35.5574    ,  43.1096    ,  34.4751    ,
          42.0554    ,  40.3944    ,  40.2466    ,  32.2567    ,
          39.58737451,  38.8594    ,  43.947     ,  41.7973    ,
          41.8105    ,  40.3797    ,  31.2868    ,  45.3644    ,
          40.7177    ,  41.8558    ,  38.9249    ,  33.2077    ,
          42.4053    ,  42.559     ])
  </code></pre>