Home > database >  Selecting Dataframe row from a condition to another condition
Selecting Dataframe row from a condition to another condition

Time:06-06

I have a dataframe with two columns:

        A      B
0   False  False
1   False  False
2   False  False
3    True  False
4   False  False
5   False  False
6   False   True
7   False  False
8   False  False
9   False  False
10   True  False
11  False  False
12  False  False

I would like to create a new column "C" with Boolean values, that turns on (=True) each time B turns on and turns of each time A turns on (ex: here between index 6 to index 10)

Ex: for this df, the output will be:

    A      B      C
0   False  False  False
1   False  False  False
2   False  False  False
3    True  False  False
4   False  False  False
5   False  False  False
6   False   True   True
7   False  False   True
8   False  False   True
9   False  False   True
10   True  False   True
11  False  False  False
12  False  False  False

I wrote this code with a for loop and a "switch", but I'm pretty sure there will be faster and easier solution to do the same thing for large dataframes. I appreciate your help.

import pandas as pd
import numpy as np

df = pd.DataFrame({
  'A': [False,False,False,True,False,False,False,False,False,False,True,False,False],
  'B': [False,False,False,False,False,False,True,False,False,False,False,False,False]
})


df["C"]=0
switch=False
for i in df.index :
  if df.B.iloc[i]:
    switch=True
  if switch:
    df.C.iloc[i]=True
  else:
    df.C.iloc[i]=False
  if df.A.iloc[i]:
    switch=False

print(df)

CodePudding user response:

Alternative approach using ffill

df.loc[df['A'],'C'] = False
df.loc[df['B'],'C'] = True

df['C'] = df['C'].ffill().fillna(False) #start "off"

CodePudding user response:

Combine the two columns, subtract 1, filter out negative and even numbers:

x = (df['A'] | df['B']).cumsum().sub(1)
df['C'] = (x >= 0) & (x % 2 == 1)

Output:

>>> df
        A      B      C
0   False  False  False
1   False  False  False
2   False  False  False
3    True  False  False
4   False  False  False
5   False  False  False
6   False   True   True  <
7   False  False   True  <
8   False  False   True  <
9   False  False   True  <
10   True  False  False
11  False  False  False
12  False  False  False
  • Related