A fast method for comparing a value in one Panda’s row to another in a previous row?
<p>I have a DataFrame, df, that looks like:</p>

<pre><code>ID    |          TERM       |   DISC_1
1     |         2003-10     |   ECON
1     |         2002-01     |   ECON
1     |         2002-10     |   ECON
2     |         2003-10     |   CHEM
2     |         2004-01     |   CHEM 
2     |         2004-10     |   ENGN
2     |         2005-01     |   ENGN
3     |         2001-01     |   HISTR
3     |         2002-10     |   HISTR 
3     |         2002-10     |   HISTR
</code></pre>

<p>ID is a student ID, TERM is an academic term, and DISC_1 is the discipline of their major.  For each student, I’d like to identify the TERM when (and if) they changed DISC_1, and then create a new DataFrame that reports when. Zero indicates they did not change. The output looks like:</p>

<pre><code>ID    |     Change
1     |         0     
2     |         2004-01    
3     |         0    
</code></pre>

<p>My code below works, but it’s very slow. I tried to do this using Groupby, but was unable to.  Could someone explain how I might accomplish this task more efficiently?</p>

<pre><code>df = df.sort_values(by = ['PIDM', 'TERM'])
c = 0
last_PIDM = 0
last_DISC_1 = 0
change = [ ]
for index, row in df.iterrows():
    c = c + 1
    if c &gt; 1:
        row['change'] = np.where((row['PIDM'] == last_PIDM) &amp; (row['DISC_1'] != last_DISC_1),     row['TERM'], 0)
        last_PIDM = row['PIDM']
        last_DISC_1 = row['DISC_1']

    else:
        row['change'] = 0
    change.append(row['change'])  

df['change'] = change        
change_terms = df.groupby('PIDM')['change'].max()
</code></pre>