Python 在数据帧函数中使用shift函数

Python 在数据帧函数中使用shift函数,python,pandas,dataframe,Python,Pandas,Dataframe,我想创建一个函数来比较数据帧中的不同行。 我当前的函数如下所示: def f(row): if row['A'].shift(1) == row['B']: val = 0 else: val = 1 return val 我收到以下错误消息: AttributeError: ("'numpy.float64' object has no attribute 'shift'", 'occurred at index 200

我想创建一个函数来比较数据帧中的不同行。
我当前的函数如下所示:

def f(row):
    if row['A'].shift(1) == row['B']:
        val = 0        
    else:
        val = 1
    return val
我收到以下错误消息:

AttributeError: ("'numpy.float64' object has no attribute 'shift'", 'occurred at index 2006-02-28 00:00:00')

我知道问题发生在数据帧的第一行,因为没有可以移位的行。有人知道如何重写函数吗?非常感谢您的帮助。

使用
pandas
您可以

(~(df.A.shift()==df.B)).astype(int) # since default of shift is 1