我想将具有某个值的行减去具有其他值的行,假设我有一个包含16行和两列的数据帧。我需要在0之前和之后的标志列中的前8个和后8个值列不同。
flag values
0 456
0 789
8 56
8 1
8 0
8 2
8 74
0 900
0 45
0 45
8 85
8 43
8 4
8 43
8 90
0 455
预期输出如下
22 (end 8 value(74) - start 8 value(56))
5 (end 8 value(90) - start 8 value(85))
答案 0 :(得分:1)
使用:
#compare 8 in flag column
m = df['flag'].eq(8)
#create consecutive groups and filter by mask
g = m.ne(m.shift()).cumsum()[m]
#aggregate last and first by groups
df = df['values'].groupby(g).agg(['last','first']).reset_index(drop=True)
#get difference
df['diff'] = df['last'] - df['first']
print (df)
last first diff
0 74 56 18
1 90 85 5