1

I have spent a while looking for a solution for this, even on stackoverflow as well as outside.

I am trying to get difference between adjacent rows but different columns.

df['Difer'] = df['Low'].diff()

I have tried to use this, with the following result.

      Dates      Open   High    Low  Close   Volume  Difer
1629 2023-07-16  36.75  36.85  36.45  36.60   744703  NaN
1630 2023-07-17  36.60  36.80  36.40  36.70  1238427 -0.05 [**1]
1631 2023-07-18  36.70  36.75  36.30  36.60  1134358 -0.10 [**2]
1632 2023-07-19  36.60  36.65  36.10  36.10  1702677 -0.20 [**3]
1633 2023-07-20  36.10  36.60  36.00  36.35  1790973 -0.10

I am trying to avoid recursive loop as in some cases i might have a couple of hundred thousand rows across multiple dataframes. Ideally vector method or numpy or pandas is the ideal solution, but have not found anything yet.

I Want to find out if any df['Low'] has value greater than previous row df['High'] or in other words

                [Low] - [High of previous row]
[**1] should be 36.40 - 36.85
[**2] should be 36.30 - 36.80
[**3] should be 36.10 - 36.75

any pointers or help is much appreciated.

Thank you.

1
  • Try df['Difer'] = df.Low.shift(-1) - df.High. Here shift will shift the Low column by one row up and then we take the difference between High and shifted Low. Commented Sep 18, 2023 at 5:00

1 Answer 1

3

You can simply shift the High values to line them up with the Low ones:

df['Difer'] = df['Low'] - df['High'].shift()

Output:

           Dates   Open   High    Low  Close   Volume  Difer
1629  2023-07-16  36.75  36.85  36.45  36.60   744703    NaN
1630  2023-07-17  36.60  36.80  36.40  36.70  1238427  -0.45
1631  2023-07-18  36.70  36.75  36.30  36.60  1134358  -0.50
1632  2023-07-19  36.60  36.65  36.10  36.10  1702677  -0.65
1633  2023-07-20  36.10  36.60  36.00  36.35  1790973  -0.65
Sign up to request clarification or add additional context in comments.

Comments