[英]How to fix 'The truth value of a Series is ambiguous' in Python produced after comparing simple data
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.从雅虎获取比特币 (BTC-USD) 的数据后,我尝试创建一个新列来显示收盘价是否高于每天的开盘价。
What I want to do is to create a column that shows 1 when the Close is higher than the Open.我想要做的是创建一个列,当收盘价高于开盘价时显示 1。 And a 0 when that condition is not true.
当该条件不成立时为 0。
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.当我尝试比较 Close 和 Open 值时,我收到下一条消息:ValueError: 系列的真值不明确。 Use a.empty, a.bool(), a.item(), a.any() or a.all().
使用 a.empty、a.bool()、a.item()、a.any() 或 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.我尝试了几种方法来修复它(例如将数据作为 CSV 导入而不是直接从雅虎导入),但不幸的是我找不到解决方案。
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.
当 Close > Open 时为 1,当条件为假时为 0。
If you want to place 1 in a column when Close > open just use np.where如果您想在 Close > open 时将 1 放在一列中,只需使用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
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.
系列比较返回具有 True/False 值的系列。 You can multiply the True by 1 to obtain your desired result.
您可以将 True 乘以 1 以获得所需的结果。
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['Close'] > df['Open']
系列上使用.apply
(我会用简单的 1/0 来展示它,但你可以从那里拿起它) :
df['X'] = (df['Close'] > df['Open']).apply(lambda x: 1 if x else 0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.