How do i filter out rows in a dataframe Based on values between 2 columns. Please refer the image. My expected result would be the rows between TRUE in Column A and TRUE in Column B. As it is highlighted in the image the expected result would be two dataframes where first dataframe df1 should have rows from index 2 to 6 and second dataframe df2 should have rows from index 10 to 16
3 Answers
Setup
df = pd.DataFrame({'other':range(9),
'A':[True ,False, False ,False, False ,True, False, False, False],
'B':[False,False,False,True,False,False,False,True,False]})
other A B
0 0 True False
1 1 False False
2 2 False False
3 3 False True
4 4 False False
5 5 True False
6 6 False False
7 7 False True
8 8 False False
Solution
df2 = df[df['A'].cumsum().ge(1)]
m1 = ~df2[['A','B']].any(axis = 1)
m2=(df2['A'].add(df2['B']).cumsum()%2).eq(1)
#m2=(df2['A'].add(df2['B']).cumsum()%2) #It could be enough
df_filtered = df2.loc[m1 & m2]
print(df_filtered)
other A B
1 1 False False
2 2 False False
6 6 False False
2 Comments
Celius Stingher
Nice masking. I was doing something more or less the same.
ansev
Thanks @Celius Stingher
If its a pandas dataframe then the following might help you:
result = df[(df['A']== 'TRUE' ) | (df['B'] == 'TRUE')]
3 Comments
Celius Stingher
OP is interested in keeping the rows between the TRUE values. Furthermore we can see TRUE/FALSE are strings, not booleans. The result should drop only the 0 and 8 row.
Ditlev Jørgensen
Where do yo usee True/False are strings?
Celius Stingher
They are written
TRUE and FALSE, boolean objects are True and False in python.