I am trying to create a new Pandas column that indicates whether a number is the largest compared to the row above and the row below.
The column is going to be binary, where '1' indicates it is the highest number compared to last and next row, and '0' will indicate the condition is not satisfied.
This is how the data looks:
Date High
0 2015-11-11 25.90
1 2015-11-12 27.12
2 2015-11-13 26.20
3 2015-11-16 26.19
4 2015-11-17 25.51
5 2015-11-18 26.31
6 2015-11-19 26.00
7 2015-11-20 27.01
8 2015-11-23 25.60
9 2015-11-24 27.00
10 2015-11-25 26.49
This is my desired outcome:
Date High higher
0 2015-11-11 25.90 0.0
1 2015-11-12 27.12 1.0
2 2015-11-13 26.20 0.0
3 2015-11-16 26.19 0.0
4 2015-11-17 25.51 0.0
5 2015-11-18 26.31 1.0
6 2015-11-19 26.00 0.0
7 2015-11-20 27.01 1.0
8 2015-11-23 25.60 0.0
9 2015-11-24 27.00 1.0
10 2015-11-25 26.49 0.0
For further illustration, here is what I did on Microsoft Excel, which is exactly what I am trying to achieve with Python: ExcelExample
First Attempt:
for i in dftest['High']:
if dftest['High'][i] > dftest['High'][i-1] and dftest['High'][i] > dftest['High'][i+1]:
dftest['higher'] = 1
else:
dftest['higher'] = 0
This results in the error: "TypeError: cannot do label indexing on class 'pandas.core.indexes.range.RangeIndex' with these indexers [25.9] of class 'float'"
Second Attempt: Here I try to make use of '.shift()'.
for i in dftest['High']:
if dftest['High'] > dftest['High'].shift(1) and dftest['High'] > dftest['High'].shift(-1):
dftest['higher'] = 1
else:
dftest['higher'] = 0
This results in error: "TypeError: unsupported operand type(s) for &: 'float' and 'float'"
Third Attempt (similar to the second attempt): Used '&' operator instead of 'and'.
Results in error: "TypeError: cannot do label indexing on class 'pandas.core.indexes.range.RangeIndex' with these indexers [25.9] of class 'float'"
Would appreciate any sort of help!
Edit: I would really appreciate if the solution provided could easily be alternated if I wanted to find the highest of 5 or 7 or 9 consecutive numbers. Thank you again!