Home > Enterprise >  Compare rows and columns to add result in new dataframe column
Compare rows and columns to add result in new dataframe column

Time:01-20

I've need to compare data in diferents columns and row and I've a complex for and if python code. I'm thinking for a bether solution but I not found any more with my limited python knowhow.

I need to compare the "Open" value with previous row "Close" and if the diference is more than X value, 3 for this example I need to add a GapUp or GapDown text in "Gap" column.

I've this code and I need to add the first "df["Gap"] = None" line to solve the for exception error:

Any ideas to make it more simple and functionally?

df["Gap"] = None
N = 3
for i in range(1,len(df)):
    if df['Open'][i] > df['Close'][i-1] and df['Open'][i] - df['Close'][i-1] > N:
        df['Gap'][i] = 'GapUp'
    elif df['Open'][i] < df['Close'][i-1] and df['Close'][i-1] - df['Open'][i] > N:
        df['Gap'][i] = 'GapDown'
    else:
        df['Gap'][i] = None
Date Open Close Gap
2018-01-17 178,90 184,36 NaN
2018-01-18 187,36 192,14 GapUp
2018-01-19 184,27 180,24 GapDown
2018-01-20 181,17 182,45 NaN
2018-01-21 183,14 187,47 NaN
2018-01-22 188,14 191,78 NaN
2018-01-23 205,41 209,17 GapUp
2018-01-24 211,02 215,27 NaN
2018-01-25 219,89 223,17 GapUp
2018-01-26 223,08 219,21 NaN
2018-01-27 212,32 206,21 GapDown
2018-01-28 205,14 203,12 NaN
2018-01-29 204,32 201,93 NaN
2018-01-30 199,45 195,15 NaN
2018-01-31 191,23 189,16 GapDown
2018-02-01 188,42 184,67 NaN
2018-02-02 183,12 186,74 NaN

CodePudding user response:

One idea would be to create a temporary column with the Previous Close then use a function to generate the Gap indicator:

def func(open, close):
    gap = open - close
    if gap >= 3.0:
        return 'GapUp'
    elif gap <= -3.0:
        return 'GapDown'
    else:
        return None
    
df['PrevClose'] = df['Close'].shift(1)
df['Gap'] = df.apply(lambda x: func(x['Open'], x['PrevClose']), axis = 1)
df.drop(['PrevClose'], axis = 1, inplace = True)
  • Related