Combining two pandas series with changing logic Combining two pandas series with changing logic pandas pandas

Combining two pandas series with changing logic


First, we know for sure that result is always False when s2 is False, and always True when both s1 and s2 are True. That does not depend on previous values:

x.loc[~x['s2'], 'result'] = Falsex.loc[x['s1'] & x['s2'], 'result'] = True

Then we fill NA's with "forward fill":

x['result'].fillna(method = 'ffill', inplace = True)

And in case there are some NA's remaining at the beginning of the column, we replace them with False:

x['result'].fillna(False, inplace = True)