Ayush Anand
Ayush Anand

Reputation: 21

Difference of 2 columns in pandas dataframe with some given conditions

enter image description here

I have a sheet like this. I need to calculate absolute of "CURRENT HIGH" - "PREVIOUS DAY CLOSE PRICE" of particular "INSTRUMENT" and "SYMBOL".
So I used .shift(1) function of pandas dataframe to create a lagged close column and then I am subtracting current HIGH and lagged close column but that also subtracts between 2 different "INSTRUMENTS" and "SYMBOL". But if a new SYMBOL or INSTRUMENTS appears I want First row to be NULL instead of subtracting current HIGH and lagged close column.
What should I do?

Upvotes: 0

Views: 72

Answers (1)

jezrael
jezrael

Reputation: 862581

I believe you need if all days are consecutive per groups:

df['new'] = df['HIGH'].sub(df.groupby(['INSTRUMENT','SYMBOL'])['CLOSE'].shift())

Upvotes: 1

Related Questions