This question already has an answer here:
I am trying to run through the dataframe and create a new column whose values are dependent on an if statement.
I have googled the error and read some answers but it does not make sense to me as to why it is not working
for i in fb:
if fb['MA10'] > fb['MA50']:
fb['Shares'] = 1
else:
fb['Shares'] = 0
fb.head(10)
here is the error I get:
>The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Open High Low Close Adj Close Volume MA10 MA50 Shares
2015-03-13 22.559999 22.760000 22.250000 22.700001 22.116024 8982200 22.648 21.0174 0
2015-03-16 22.879999 23.110001 22.730000 22.969999 22.379078 5923900 22.685 21.0758 0
2015-03-17 22.920000 23.260000 22.760000 23.250000 22.651876 7497500 22.792 21.1382 0
2015-03-18 23.250000 23.370001 22.660000 22.870001 22.281652 10337600 22.836 21.1998 0
2015-03-19 22.950001 23.299999 22.780001 23.219999 22.622650 7768900 22.872 21.2804 0
2015-03-20 23.410000 23.610001 23.190001 23.469999 22.866215 11552100 22.964 21.3670 0
2015-03-23 22.770000 23.070000 22.700001 22.709999 22.125767 10595700 22.974 21.4240 0
2015-03-24 22.639999 22.799999 22.299999 22.400000 21.823744 8262000 22.914 21.4732 0
2015-03-25 22.389999 22.389999 20.969999 21.040001 20.498730 13563800 22.730 21.5002 0
2015-03-26 20.610001 21.059999 20.459999 20.969999 20.430529 7798000 2 2.560 21.5264 0
Here I have modified my code by inserting .any() here:
if fb['MA10'].any() > fb['MA50'].any():
but as you can see the issue becomes that I get zero's for all my 'Shares' column when I should not be.
Aucun commentaire:
Enregistrer un commentaire