import pandas as pd
data={\'col1\':[1,3,3,1,2,3,2,2]}
df=pd.DataFrame(data,columns=[\'col1\'])
print df
col1
0 1
1 3
1) pandas approach: Use diff:
df['match'] = df['col1'].diff().eq(0)
2) numpy approach: Use np.ediff1d.
df['match'] = np.ediff1d(df['col1'].values, to_begin=np.NaN) == 0
Both produce:
Timings: (for the same DF used by @jezrael)
%timeit df.col1.eq(df.col1.shift())
1000 loops, best of 3: 731 µs per loop
%timeit df['col1'].diff().eq(0)
1000 loops, best of 3: 405 µs per loop