我有一个这样的DataFrame:
.data
tree: .word n01
n01: .word 0, n02, n03
n02: .word 0, n04, n05
n03: .word 0, n06, n07
n04: .word 0, 0, 0
n05: .word 0, 0, 0
n06: .word 1, 0, 0
n07: .word 0, 0, 0
.text
lw a0, tree
jal altezza
addi a0, a0, -1
li a7, 1
ecall
li a7, 10
ecall
altezza:
bne a0, zero, altezza_ric
jalr zero, ra, 0
altezza_ric:
addi sp, sp, -12
lw t1, 0(a0)
sw ra, 0(sp)
bne t1, zero, skip_numb
sw a0, 4(sp)
lw a0, 4(a0)
jal altezza
sw a0, 8(sp)
lw a0, 4(sp)
lw a0, 8(a0)
jal altezza
lw t0, 8(sp)
bne a0, zero, scelta
mv a0, t0
bne a0, zero, scelta
jal skip
scelta:
ble a0,t0,skip_add
mv a0, t0
skip_add:
addi a0, a0, 1 #ad a0 inremento 1
skip: lw ra, 0(sp)
addi sp, sp, 12
jalr zero, ra, 0
skip_numb:
add a0, zero, zero
jal skip_add
我试图为此定义一个函数,但是它不起作用:
subject trial attended
0 1 1 1
1 1 3 0
2 1 4 1
3 1 7 0
4 1 8 1
5 2 1 1
6 2 2 1
7 2 6 1
8 2 8 0
9 2 9 1
10 2 11 1
11 2 12 1
12 2 13 1
13 2 14 1
14 2 15 1
我认为我不清楚如何在每个GroupBy数据帧内进行迭代。我刚开始使用熊猫。
答案 0 :(得分:5)
IIUC试试,
df = df.query('attended == 1')
df.loc[df.groupby('subject')['attended'].cumsum() == 4, ['subject', 'trial']].to_dict(orient='record')
输出:
[{'subject': 2, 'trial': 9}]
将groupby
与cumsum
一起使用将进行计数,然后检查该值何时等于4以创建布尔序列。您可以使用此布尔系列进行布尔索引,以将数据框过滤到某些行。最后,通过锁定和列过滤选择主题和审判。