Reputation: 117
I need some help.
I have a dataframe with a int column. I want to test this values.
test1 = (data['CC'].shift(1) > 50) & (data['CC'] < 50)
test2 = (data['CC'].shift(1) < -50) & (data['CC'] > -50)
This code works and return a Series of 2000 rows. I have no problem with this.
0 False
1 False
2 False
3 False
4 False
Name: CCI, dtype: bool
I want to use this Series to add a new column.
if(test1):
data['Sloop'] = '+S'
elif(test2):
data['Sloop'] = '-S'
else:
data['Sloop'] = 'N'
The if statement is not working:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Of course, the test1 and test2 exists here. The real code is:
if((data['CC'].shift(1) > 50) & (data['CC'] < 50)):
data['Sloop'] = '+S'
elif((data['CC'].shift(1) < -50) & (data['CC'] > -50)):
data['Sloop'] = '-S'
else:
data['Sloop'] = 'N'
Same error. Please, some help.
Thanks
Upvotes: 1
Views: 57
Reputation: 863751
Use numpy.select
:
test1 = (data['CC'].shift(1) > 50) & (data['CC'] < 50)
test2 = (data['CC'].shift(1) < -50) & (data['CC'] > -50)
data['Sloop'] = np.select([test1, test2], ['+S','-S'], default='N')
Upvotes: 1