How can I change logic from (False to True) based on the 1st column and (True to False) based on the 2nd column

51 views Asked by At

The idea is to switch between the set of True and False conditioned by 2 columns.

For example

Column A Column B Column C
False False False
False False False
True False True
False False True
True False True
True False True
True False True
False False True
False True False
False True False
False False False
False False False
False True False
True False True
False False True
False False True

What I would like to generate is the "Column C".

"Column A" changes "Column C" from ("False" to "True").

"Column B" changes "Column C" from ("True" to "False").

Triggering by the First "True"

1

There are 1 answers

0
Omegaruins On

I finally have the answer to this by making use of cumsum().

df['Column A'] = ....
df['Column B'] = ....

df["Cumsum1"] = df['Column A'].cumsum()
df["Cumsum2"] = df["Cumsum1"].where(df["Column B"]).ffill().fillna(0).astype(int)

df["Column C"] = df["Cumsum1"]-df["Cumsum2"]
df["Column C"] = df["Column C"].replace(df["Column C"].values, 1).where(df["Column C"]!=0, 0).astype(bool)

If anyone have the simpler solution, please share. Thanks