I have the following DataFrame:
As you can see, the column hist_sol_folio
has an array with several elements separated by commas. What I need to do is manage to iterate over that column and if in any record it finds the element "QO"
that returns a 1, otherwise a 0
, for which I perform a list comprehension as follows:
quebranto_lambda = lambda lista: [1 for i in lista if i == 'QO']
for i in claims.select('hist_sol_folio','issuing_entity_id').collect():
print(i[1])
print(i[0])
print(quebranto_lambda(i[0]))
Which is only partially correct, since it returns a 1
when it finds the element "QO"
but returns an empty list otherwise, not one 0
as I need.
Does anyone know how I could supplement or change the code? It is not necessary to use list comprehension, in fact if it returns only the number (1 or 0) and not a list it is better but it occurred to me to do it this way.
Basically what you want to know is simply whether
'Q0'
or not it is on the list, and that information is given to you by the operatorin
:which will return you
True
if it is, orFalse
if not.If you want the answer to be 1 or 0, just add 0 to said answer, because in that case Python will be forced, to do the addition, to treat the boolean as an integer, for which it will convert
True
to 1 andFalse
0 (and adding 0 to them does not change the result).So your lambda could be: