Reputation: 523
after getting data of the Bitcoin (BTC-USD) from Yahoo, I try to create a new column to show if the Close price is higher than the Open price of of each day.
What I want to do is to create a column that shows 1 when the Close is higher than the Open. And a 0 when that condition is not true.
When I try to compare the Close and the Open values I receive the next message : ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
I have tried several things to fix it (like importing the data as a CSV instead directly from Yahoo) but unfortunately I couldn't find the solution.
Below you can see the code that I am using:
import pandas as pd
import pandas_datareader as dr
df = dr.data.get_data_yahoo('btc-usd',start = '01-01-2015', end= '31-12-2018')
df.head(2)
df['X'] = [1 if (df.loc[ei,'Close'] > df.loc[ei,'Open']) else 0 for ei in df.index] #---> The error is produced in this line of code
df.tail()
Below you can see the error message:
ValueError Traceback (most recent call last)
<ipython-input-45-eb64775bf24f> in <module>
----> 1 df['X'] = [1 if (df.loc[ei,'Close'] > df.loc[ei,'Open']) else 0 for ei in df.index]
2 df.tail()
<ipython-input-45-eb64775bf24f> in <listcomp>(.0)
----> 1 df['X'] = [1 if (df.loc[ei,'Close'] > df.loc[ei,'Open']) else 0 for ei in df.index]
2 df.tail()
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py in __nonzero__(self)
1476 raise ValueError("The truth value of a {0} is ambiguous. "
1477 "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
-> 1478 .format(self.__class__.__name__))
1479
1480 __bool__ = __nonzero__
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
I would like to get the result of this formula. Having 1 when the Close is > Open and 0 when the conditional is false.
Upvotes: 1
Views: 688
Reputation: 2905
You can do the series comparison outside of the list comprehension, like this:
df['X'] = 1*(df['Close'] > df['Open'])
It'll work. The series comparison returns a series with True/False values. You can multiply the True by 1 to obtain your desired result.
If you need a more complicated function, you can use an .apply
on the df['Close'] > df['Open']
series (I'll show it with simple 1/0 but you can pick it up from there):
df['X'] = (df['Close'] > df['Open']).apply(lambda x: 1 if x else 0)
Upvotes: 0
Reputation: 3739
If you want to place 1 in a column when Close > open just use np.where
import numpy as np
df['X'] = np.where(df['Close']>df['Open'],1,0)
second solution could be
df['X'] = 0
df.loc[df['Close']>df['Open'],"X"] = 1
print(df)
High Low Open Close Volume Adj Close X
Date
2014-12-31 319.089996 308.890015 311.269989 318.239990 6472822 318.239990 1
2015-01-01 321.359985 313.540009 318.239990 314.890015 4073067 314.890015 0
2015-01-02 316.399994 313.079987 314.890015 315.209991 4673971 315.209991 1
2015-01-03 315.829987 284.890015 315.209991 287.130005 14209564 287.130005 0
2015-01-04 289.940002 255.869995 287.130005 264.720001 24255392 264.720001 0
Upvotes: 1