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"
CodePudding user response:
Here is an example of an implementation you can use. First I've created a dataframe containing the sample data you provided:
import pandas as pd
import numpy as np
col_a_data = [False, False, True, False, True, True, True,
False, False, False, False, False, True, False, False]
col_b_data = [False, False, False, False, False, False, False,
True, True, False, False, True, False, False, False]
data = pd.DataFrame({'Column A': col_a_data,
'Column B': col_b_data})
Next, use the create a new column that switches between True and False based on the values in Column A and Column B.
I am using the np.where()
function to control the logic:
np.where(condition, x, y)
- checks the condition, if it's True then it returns x else y.
data['Column C'] = np.where(data['Column A'] == True,
True,
np.where(data['Column B'] == True,
False,
data['Column C']))
CodePudding user response:
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_ohlcv["Column B"]).ffill().fillna(0).astype(int)
df_ohlcv["Column C"] = df_ohlcv["Cumsum1"]-df_ohlcv["Cumsum2"]
df_ohlcv["Column C"] = df_ohlcv["Column C"].replace(df_ohlcv["Column C"].values, 1).where(df_ohlcv["Column C"]!=0, 0).astype(bool)
If anyone have the simpler solution, please share. Thanks