Reputation: 278
I want to create a function which compares different rows within a Pandas dataframe.
My current function looks like this:
def f(row):
if row['A'].shift(1) == row['B']:
val = 0
else:
val = 1
return val
I receive the following error message:
AttributeError: ("'numpy.float64' object has no attribute 'shift'", 'occurred at index 2006-02-28 00:00:00')
I understand that the problem occurs in the first row of the dataframe because there is no row which can be shifted. Does anyone know how to rewrite the funtion? Any help is appreciated.
Upvotes: 1
Views: 1090
Reputation: 323226
With pandas
you can .
(~(df.A.shift()==df.B)).astype(int) # since default of shift is 1
Upvotes: 1