I am trying to create a new Column by comparing the value row with its previous value error that I get is ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). I have checked the data type of columns. All of them are float64
But I am getting an error CODE:
cols=['High', 'Low', 'Open', 'Volume', "Adj Close"]
df = df.drop(columns = cols)
df['EMA60'] = df['Close'].ewm(span=60, adjust=False).mean()
df['EMA100'] = df['Close'].ewm(span=100, adjust=False).mean()
df['MACD_60_100'] = df['EMA60'] - df['EMA100']
df['SIGNAL_60_100'] = df['MACD_60_100'].ewm(span=9, adjust=False).mean()
df['HIST_60_100'] = df['MACD_60_100'] - df['SIGNAL_60_100'] # Histogram
df = df.iloc[1: , :] # Delete first row in DF as it contains NAN
print(df.dtypes)
print (df)
if df[df['HIST_60_100'] > df['HIST_60_100'].shift( 1)]: # check if the valus is > previous row value
df['COLOR-60-100'] = "GREEN"
else:
df['COLOR-60-100'] = "RED"
print(df.to_string())
ERROR:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_10972/77395577.py in <module>
32
33
---> 34 get_data_from_yahoo(symbol ".NS")
35
36 # df.to_excel(sheetXls, index=False)
~\AppData\Local\Temp/ipykernel_10972/520355426.py in get_data_from_yahoo(symbol, interval, start, end)
26 print(df.dtypes)
27 print (df)
---> 28 if df[df['HIST_60_100'] > df['HIST_60_100'].shift( 1)]:
29 df['COLOR-60-100'] = "GREEN"
30 else:
~\AppData\Roaming\Python\Python39\site-packages\pandas\core\generic.py in __nonzero__(self)
1327
1328 def __nonzero__(self):
-> 1329 raise ValueError(
1330 f"The truth value of a {type(self).__name__} is ambiguous. "
1331 "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
CodePudding user response:
No need for an if.. else
statement here. You can use numpy.where
instead.
numpy.where(condition, [x, y, ]/)
Replace this :
if df[df['HIST_60_100'] > df['HIST_60_100'].shift( 1)]: # check if the valus is > previous row value
df['COLOR-60-100'] = "GREEN"
else:
df['COLOR-60-100'] = "RED"
By this :
df['COLOR-60-100'] = np.where(df['HIST_60_100'].gt(df['HIST_60_100'].shift(1), "GREEN", "RED")