Python: logical comparing with columns in panda's dataframe
<p>I have a dataframe where I want to determine when the <code>ser_no</code> and <code>CTRY_NM</code> are the same and differ. However, I want to be mindful of the <code>ser_no</code> changes and not make a false and false return true or a false/true return false.</p>

<p>Consider the following dataframe:</p>

<pre><code>import pandas as pd
df = pd.DataFrame({'ser_no': [1, 1, 1, 2, 2, 2, 2, 3, 3, 3],
                'CTRY_NM': ['a', 'a', 'b', 'e', 'e', 'a', 'b', 'b', 'b', 'd']})
def check(key):
    return df[key] == df[key].shift(1)

match = check('ser_no') == check('CTRY_NM')
</code></pre>

<p>This returns:</p>

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

<p>However, at indices, 4 and 8 we have serial number changes. Since each serial number is a different machine, it doesn't make sense to have a logical comparison at these locations. When <code>ser_no</code> changes, how can I insert <code>NaN</code> instead of do a logical comparison?</p>