基于列的行切片

Column Based Row Slices

问题如下。我有一个如下所示的数据框。我想根据行

进行切片
Index A B
0 0 1
1 0 1
2 1 0
3 1 0
4 1 0
5 1 0
6 0 1
7 0 1
8 1 0
9 1 0
10 0 1

我想根据特定的行组合对行进行切片。例如下面

Index A B
2 1 0
6 0 1
8 1 0
10 0 1

所以切片组合应该是,

A B
1 0
0 1

尝试了一些多索引切片、行差异但无法在产品数据帧之上。对此的任何帮助表示赞赏。谢谢

试试这个

import pandas as pd

d = {'A' : [0,0,1,1,1,1,0,0,1,1,0],
'B' : [1,1,0,0,0,0,1,1,0,0,1]
}
df = pd.DataFrame(data = d)

print(df)

list_a = []
list_b = []
test = 1
for x in range(0, len(df['A'])):
    if (df['A'][x] == 1 and test == 1):
        list_a += [1]
        list_b += [0]
        test = 0 
    elif (df['B'][x] == 1 and test == 0):
        list_b += [1]
        list_a += [0]
        test = 1
    elif (df['A'][x] == 1 and x == 0):
        list_a += [1]
        list_b += [0]

new_d = {'A' : list_a,
         'B' : list_b}
new_df = pd.DataFrame(data = new_d)

print("________BREAK__________\n\n")
print(new_df)

输出

   A  B
0  1  0
1  0  1
2  1  0
3  0  1