Given a dataframe:
d = {'A': [2, 1, 4, 5, 7, 8, 7, 5], 'B': [5, 7, 7, 6, 10, 9, 12, 10]}
testdf = pd.DataFrame(data=d)
A B
0 2 5
1 1 7
2 4 7
3 5 6
4 7 10
5 8 9
6 7 3
7 5 2
I'm comparing both columns and I expect to append 'Inside' to array if A > A-1 AND B < B-1, otherwise append 'Broken'.
array = []
for i in range(1,len(testdf)):
if testdf.A[i] > testdf.A[i-1]:
if testdf.B[i] < testdf.B[i-1]:
array.append('INSIDE')
else:
array.append('BROKEN')
The result is:
['BROKEN', 'INSIDE', 'BROKEN', 'INSIDE']
But I expect:
['BROKEN', 'BROKEN', 'INSIDE', 'BROKEN', 'INSIDE', 'BROKEN', 'BROKEN']
I tried different variations with the starting point of the loop
for i in range(len(testdf)-1):
but it causes only key errors
How to improve the code to get it running as expected?