3
I’m trying to filter lines in which the columns comply with conditions consecutively.
That is, if the row has columns with the conditions of after an L/I, the next column has a A/S, then return the value of 1
in the new column (if no, return 0
)
Input:
RFA RFB RFC RFD
0 S S S S
1 A I A A
2 A A L A
4 S S L A
Output:
RFA RFB RFC RFD promo
0 S S S S 0
1 A I A A 1
2 A A L A 1
4 S S L A 1
Script:
def promo_behaviour(x):
for i in range(0,95411):
for j in data_rfa_r.columns:
if (x[j][i] == 'L' or x[j][i] == 'I') and (x[j][i+1] == 'A' or x[j][i+1] == 'S'):
return 1
else:
return 0
data_rfa_r['promo'] = data_rfa_r.apply(promo_behaviour)
I wrote this function but without success (95411 are the number of remarks/lines).
I forgot to mention that in the context of the problem, the index column 0 is the latest! I mean, it should be read from right to left.
EDIT:
Output:
RFA promo2 RFB promo1 RFC RFD
0 S 0 S 0 S S
1 A 1 I 0 A A
2 A 0 A 1 L A
4 S 0 S 0 L A
Good afternoon! In the actual database no! but there are more than 25 variables( --> 25 columns)...
– zoramind