0

enter image description here

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 3

2

Since you require rows between the two conditions.

df = pd.read_excel("your_excel.xls")
start_index = df[df["A"] == "TRUE"].index[0]
end_index = df[df["B"] == "TRUE"].index[0]
df = df.iloc[start_index : end_index]
Sign up to request clarification or add additional context in comments.

Comments

1

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

Nice masking. I was doing something more or less the same.
Thanks @Celius Stingher
0

If its a pandas dataframe then the following might help you:

result = df[(df['A']== 'TRUE' ) | (df['B'] == 'TRUE')]

3 Comments

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.
Where do yo usee True/False are strings?
They are written TRUE and FALSE, boolean objects are True and False in python.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.