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. Here's an example of what the initial dataframe looks like.
In [2]: df
Out[2]:
Product Sales Quarter
0 Shoes 25 12017
1 Shoes 3 22017
2 Shoes 6 32017
3 Shoes 23 42017
4 Shoes 11 12018
5 Shoes 12 22018
6 Shoes 9 32018
7 Shoes 45 42018
8 Shoes 3 12019
9 Jacket 12 12017
10 Jacket 10 22017
...
16 Jacket 23 42018
17 Jacket 44 12019
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 (like shown below), but I receive the error instead.
Product Sales Quarter Growth
0 Shoes 25 12017 NaN
1 Shoes 3 22017 NaN
2 Shoes 6 32017 NaN
3 Shoes 23 42017 NaN
4 Shoes 11 12018 NaN
5 Shoes 12 22018 NaN
6 Shoes 9 32018 NaN
7 Shoes 45 42018 NaN
8 Shoes 3 12019 -.54
9 Jacket 12 12017 NaN
10 Jacket 10 12017 NaN
...
16 Jacket 23 42018 NaN
17 Jacket 44 12019 ##
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