Reputation: 13
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
Upvotes: 0
Views: 71
Reputation: 30930
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
Upvotes: 1
Reputation: 1086
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]
Upvotes: 2
Reputation: 43
If its a pandas dataframe then the following might help you:
result = df[(df['A']== 'TRUE' ) | (df['B'] == 'TRUE')]
Upvotes: 0