擅长:python、mysql、java
<p>对于我的作品,与<a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.shift.html" rel="nofollow noreferrer">^{<cd1>}</a>相比,则<code>NaN</code>被替换<code>0</code>,因为在没有值之前:</p>
<pre><code>df['diff'] = (df.ColumnB != df.ColumnB.shift()).astype(int)
df.ix[0,'diff'] = 0
print (df)
ColumnA ColumnB diff
0 1 Blue 0
1 2 Blue 0
2 3 Red 1
3 4 Red 0
4 5 Yellow 1
</code></pre>
<p>由<a href="https://stackoverflow.com/a/40348884/2901002">timings</a>编辑另一个答案-最快的是使用<code>ne</code>:</p>
<pre><code>df['diff'] = (df.ColumnB.ne(df.ColumnB.shift())).astype(int)
df.ix[0,'diff'] = 0
</code></pre>