1

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.

1 Answer 1

1

With pandas you can .

(~(df.A.shift()==df.B)).astype(int) # since default of shift is 1 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.