Replace cell value in pandas dataframe where value is 'NaN' with value from another/same dataframe
<p>The following pandas dataframe df1 was generated:</p>

<pre><code>df1 = pd.DataFrame(data = {'Value': [1.989920, 'NaN', -9.363819, 'NaN'], 'Group-Index' : [6, 6, 7, 7], 'Group-Order' : [2, 2, 2, 2], 'Index' : [221, 225, 222, 222] })

            Value       Group-Index Group-Order     Index
221         1.989920    6           2               221
225         NaN         6           2               225
222         -9.363819   7           2               222
278         NaN         7           2               222
</code></pre>

<p>beware that the pandas index varies because I've used the dataframe output from my actual project.</p>

<p>and there is a second dataframe df2 available which looks as follows:</p>

<pre><code>df2 = pd.DataFrame({'Value': [1.989920, -9.363819], 'Group-Index' : [6, 7], 'Group-Order' : [2, 2], 'Index' : [221, 222] })

        Value       Group-Index     Group-Order     Index
221     1.989920    6               2               221
222     -9.363819   7               2               222
</code></pre>

<ol>
<li><p>How can I search through the GC-Value column in the first dataframe and find all NaN values and then replace them with the value from the second dataframe where the Group-Index and the Group-Order column are the same in both rows of both dataframes?</p></li>
<li><p>Another solution to my problem would be to copy the value from the row where a value is defined to the NaN-cell matching with the Group-Index and Group-Order within the same dataframe df1.</p></li>
</ol>

<p>Thus, the result should be:</p>

<pre><code>            Value       Group-Index Group-Order     Index
221         1.989920    6           2               221
225         1.989920    6           2               225
222         -9.363819   7           2               222
278         -9.363819   7           2               222
</code></pre>

<pre><code>vnull = df1.Value.isnull()
mrg_cols = ['Group-Index', 'Group-Order']
df1.loc[vnull, 'Value'] = df2.merge(df1.loc[vnull, mrg_cols]).Value.values

df1
</code></pre>

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

python pandas