fill NaN with another lookup table
<p>Is there a way to fill the <code>NaN</code> with value for <code>test=default</code> by matching name, reticle and cell rev?</p>

<p><a href="http://i.stack.imgur.com/8gZ38.png" rel="nofollow"><img src="http://i.stack.imgur.com/8gZ38.png" alt="enter image description here"></a></p>

<p><a href="http://i.stack.imgur.com/A71KG.png" rel="nofollow"><img src="http://i.stack.imgur.com/A71KG.png" alt="enter image description here"></a></p>

<p>with the few variables in "test" column:
<a href="http://i.stack.imgur.com/MYDOO.png" rel="nofollow"><img src="http://i.stack.imgur.com/MYDOO.png" alt="enter image description here"></a></p>

<p>Is there a way to update the values from others row ? as the datatype "do" would have higher precedence than int and drop the "do" data row?</p>

<p>data:<br>
test  datatype name value   reticle cell_rev<br>
default int s   0x45        CR1<br>
default int s   0xCB        CR3<br>
default do  s   0.68        CR1 </p>

<p>I'd like to get:  </p>

<p>test  datatype name value   reticle cell_rev<br>
default int s   0.68        CR1<br>
default int s   0xCB        CR3  </p>

<p>You can use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.set_index.html" rel="nofollow"><code>set_index</code></a> with <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.unstack.html" rel="nofollow"><code>unstack</code></a> for reshaping, then <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.ffill.html" rel="nofollow"><code>ffill</code></a> for add missing values and last reshape to original by <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.stack.html" rel="nofollow"><code>stack</code></a>:</p>

<pre><code>df = df.set_index(['name','value_old','reticle','test','cell_rev'])
       .unstack()
       .ffill()
       .stack()
       .reset_index()

print (df)
  name value_old reticle     test cell_rev value_new
0    s      0x8E     A28  default      CR1      0x8C
1    s      0x8E     A28  default      CR3      0x8E
2    s      0x8E     A28     etlc      CR1      0x8C
3    s      0x8E     A28     etlc      CR3      0x8E
</code></pre>

<p>EDIT by comment:</p>

<p>Use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.merge.html" rel="nofollow"><code>merge</code></a> by subset <code>df1</code> created by <a href="http://pandas.pydata.org/pandas-docs/stable/indexing.html#boolean-indexing" rel="nofollow"><code>boolean indexing</code></a> and then fill <code>NaN</code> values by <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.combine_first.html" rel="nofollow"><code>combine_first</code></a> or <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.fillna.html" rel="nofollow"><code>fillna</code></a>:</p>

<pre><code>df1 = df.ix[df.test == 'default']
print (df1)     
      test name value_old reticle cell_rev value_new
0  default    s      0x8E     A28      CR1      0x8E
1  default    s      0x8E     A28      CR3      0x8C

df2 = pd.merge(df, df1, how='left', on=['name','reticle','cell_rev'], suffixes=('','1'))
print (df2)
      test name value_old reticle cell_rev value_new    test1 value_old1  \
0  default    s      0x8E     A28      CR1      0x8E  default       0x8E   
1  default    s      0x8E     A28      CR3      0x8C  default       0x8E   
2     etlc    s      0x8E     A28      CR1      0x44  default       0x8E   
3     etlc    s      0x8E     A28      CR3      0x44  default       0x8E   
4      mlc    s      0x1E     A28      CR1       NaN  default       0x8E   
5      mlc    s      0x1E     A28      CR3       NaN  default       0x8E   
6      slc    s      0x2E     A28      CR1       NaN  default       0x8E   
7      slc    s      0x2E     A28      CR3       NaN  default       0x8E   

  value_new1  
0       0x8E  
1       0x8C  
2       0x8E  
3       0x8C  
4       0x8E  
5       0x8C  
6       0x8E  
7       0x8C 
</code></pre>



<pre><code>df['value_new'] = df2['value_new'].combine_first(df2['value_new1'])
#df['value_new'] = df2['value_new'].fillna(df2['value_new1'])
print (df)
      test name value_old reticle cell_rev value_new
0  default    s      0x8E     A28      CR1      0x8E
1  default    s      0x8E     A28      CR3      0x8C
2     etlc    s      0x8E     A28      CR1      0x44
3     etlc    s      0x8E     A28      CR3      0x44
4      mlc    s      0x1E     A28      CR1      0x8E
5      mlc    s      0x1E     A28      CR3      0x8C
6      slc    s      0x2E     A28      CR1      0x8E
7      slc    s      0x2E     A28      CR3      0x8C
</code></pre>

python pandas dataframe multiple-columns