Compare columns of Pandas dataframe for equality to produce True/False, even NaNs
<p>I have two columns in a pandas dataframe that are supposed to be identical. Each column has many NaN values.  I would like to compare the columns, producing a 3rd column containing True / False values; <em>True</em> when the columns match, <em>False</em> when they do not.</p>

<p>This si what I have tried:</p>

<pre><code>df['new_column'] = (df['column_one] == df['column_two'])
</code></pre>

<p>The above works for the numbers, but not the NaN values. </p>

<p>I know I could replace the NaNs with a value that doesn't make sense to be in each row (for my data this could be -9999), and then remove it later when I'm ready to echo out the comparison results, however I was wondering if there was a more pythonic method I was overlooking.</p>

<p>Or you could just use the <code>equals</code> method:</p>

<pre><code>df['new_column'] = df['column_one'].equals(df['column_two'])
</code></pre>

<p>It is a batteries included approach, and will work no matter the <code>dtype</code> or the content of the cells. You can also put it in a loop, if you want.</p>

python pandas dataframe