vendredi 1 novembre 2019

How would you make the condition of an if statement If a record is equal to itself offset by x records?

I'm defining a simple if xxxx return y - else return NaN function. If the record, ['Product'], equals ['Product'] offset by 8 then the if condition is true.

I've tried calling the record and setting it equal to itself offset by 8 using == and .shift(8). ['Product'] is a string and ['Sales'] is an integer.

def Growth (X):
    if X['Product'] == X['Product'].shift(8):
        return (1+ X['Sales'].shift(4)) / (1+ X['Sales'].shift(8) - 1)
    else:
        return 'NaN'

I expect the output to be NaN for the first 8 records, and then to have numbers at record 9, but I receive the error instead.

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Aucun commentaire:

Enregistrer un commentaire