0

Reading from yahoo finance download ohlcv for nvidia, I am creating a column for signal buy/dontbuy, when I try to define which passes the avg>volume test everything either comes out all 'buy' or don't buy.

df=pd.read_csv('NVDA.csv',dtype={'label':str})
df['Price%delta']=((df['Close']/df['Open'])*100)                       

df['Avg_volume']=df['Volume'].rolling(7).mean()

df['Signal']=0

for index, row in df.iterrows():
    if row['Volume'] > row['Avg_volume']:
    df['Signal']='Buy'
    else:
        df['Signal']='Dont Buy'
J.J.
  • 75
  • 8

3 Answers3

1

You are not specifying any index where to assign 'Buy' or 'Don't buy'. Use loc instead:

for index, row in df.iterrows(): 
    if row['Volume'] > row['Avg_volume']:
        df.loc[index, 'Signal']='Buy'
    else:
        df.loc[index, 'Signal']='Dont Buy'
yatu
  • 86,083
  • 12
  • 84
  • 139
  • it finally worked thank you nixon – J.J. Nov 30 '18 at 20:37
  • Good to know. Please let me know iI solved it for you by setting the answer as correct. – yatu Nov 30 '18 at 20:38
  • Just an FYI - this solution will be considerably slower than the vectorized solution I have posted for larger dataframes. – rahlf23 Nov 30 '18 at 20:44
  • I know I'm aware @rahlf23 it can be vectorized, however, given that the user is unaware of how to properly index data, i found more appropriate to give some help on correcting the actual code – yatu Nov 30 '18 at 20:47
  • Not saying your answer is incorrect @nixon, it definitely produces the desired output for the OP. Just pointing something out :) – rahlf23 Nov 30 '18 at 20:49
  • Yes appreciate the comment, just saying that I find the simpler (although not efficient) solution to be more helpful in this case for OP :) – yatu Nov 30 '18 at 20:52
1

You don't really need the for loop at all:

mask = df["Volume"] > df["Avg_volume"] 

df.loc[mask, "Signal"] = "Buy"
df.loc[~mask, "Signal"] = 'Don't buy'
Christian Sloper
  • 7,440
  • 3
  • 15
  • 28
1

A vectorized solution using np.where():

df['Signal'] = np.where(df['Volume'] > df['Avg_volume'], 'Buy', 'Dont Buy')
rahlf23
  • 8,869
  • 4
  • 24
  • 54