Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
column math
#1
I have 3 columns in question... ['Last'] is data... ['Goal'] is Last shifted forward 5 rows
I am trying to assign a value to ['direction'] based on ['Goal'} >< 5

I'm probably over-complicating things, any help?

# I begin with creating a zero column as I was getting length errors
data['direction'] = 0
data['Goal'] = data['Last'].shift(5)
data = data.dropna()  #Drop rows with NAN after shift

for row in data.Last:
    if (data.values['Goal'] - data.values['Last'] >= 5):
        data['direction'] = 1, 
    elif (data.values['Goal'] - data.values['Last'] <= -5):
        data['direction'] = -1, 
    else:
        data['direction']=0
Error:
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
Reply
#2
You don't really need to initialize direction column.

data.loc[data.Goal.shift(5) - data.Last >= 5, 'direction'] = 1
data.loc[data.Goal.shift(5) - data.Last <= -5, 'direction'] = -1
data.direction.fillna(0, inplace=True) # optionally, instead of initialization
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  math.log versus math.log10 stevendaprano 10 2,301 May-23-2022, 08:59 PM
Last Post: jefsummers
  Why getting ValueError : Math domain error in trig. function, math.asin() ? jahuja73 3 3,704 Feb-24-2021, 05:09 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020