I have been experiencing several problems when trying to create a Categories column in my dataframe based on if-statements linked to one of the other columns.
My dataframe (called 'kio') looks as follows when calling the .tail() function:
High Low Open Close Volume Adj Close Daily % Change
Date
2021-12-07 48326.0 46566.0 47000.0 48245.0 300090.0 48245.0 6.168302
2021-12-08 49662.0 46662.0 49870.0 46799.0 304657.0 46799.0 -2.997202
2021-12-09 47395.0 45045.0 47001.0 45332.0 267214.0 45332.0 -3.134682
2021-12-10 46899.0 43850.0 46899.0 43997.0 221268.0 43997.0 -2.944940
2021-12-13 46285.0 44150.0 46600.0 44150.0 208391.0 44150.0 0.347751
The final column ('Daily % Change') I added myself using the following code:
for n in kio['Close']:
kio['Daily % Change'] = kio['Close'].pct_change() * 100
Not sure if the format or dtype of this column is not causing an issue?
What I am trying to do is have every element in the 'Daily % Change' column be categorised as a number between 1 and 6 (absolute values). I have attempted this through thee below code:
def mvt_cat(row = kio):
if row[(row['Daily % Change'] > 0) & (row['Daily % Change'] < 1)]:
val = 1
elif row[(row['Daily % Change'] < 0) & (row['Daily % Change'] > -1)]:
val = -1
elif row[(row['Daily % Change'] >= 1) & (row['Daily % Change'] < 2)]:
val = 2
elif row[(row['Daily % Change'] <= -1) & (row['Daily % Change'] > 2)]:
val = -2
elif row[(row['Daily % Change'] >= 2) & (row['Daily % Change'] < 3)]:
val = 3
elif row[(row['Daily % Change'] <= -2) & (row['Daily % Change'] > -3)]:
val = -3
elif row[(row['Daily % Change'] >= 3) & (row['Daily % Change'] < 4)]:
val = 4
elif row[(row['Daily % Change'] <= -3) & (row['Daily % Change'] > 4)]:
val = -4
elif row[(row['Daily % Change'] >= 4) & (row['Daily % Change'] < 5)]:
val = 5
elif row[(row['Daily % Change'] <= -4) & (row['Daily % Change'] > -5)]:
val = -5
elif row[(row['Daily % Change'] >= 5)]:
val = 6
else:
val = -6
return val
kio['Move Category'] = kio.apply(mvt_cat, axis=1)
I unfortunately get various errors - the mains ones are these:
KeyError: False ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
I have tried the possible solutions found on links such as Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all() and KeyError: False in pandas dataframe
Any assistance will be greatly appreciated!