如何仅选择val大于 5的行,直到每个id示例数据框中的最后一条记录?df = pd.DataFrame({'id': [1,1,1,1,1,1,2,2,2,2,2,2], 'val': [10,1,1,10,20,30,1,1,1,12,17,28]})id val1 10 <- meets the condition, but condition fails in the next 2 rows1 11 11 10 <- meets the condition until the end of this id1 201 302 12 12 12 122 172 28期望的输出:id val1 101 201 302 122 172 28如果只有一个 id,我可以用一些难看的代码来做到这一点,但我不知道如何将类似的逻辑应用于所有组:df = pd.DataFrame({'id': [1,1,1,1,1,1], 'val': [10,1,1,10,20,30]})# create groups at breakpoints where condition is no longer metg = df.groupby((df['val'] > 5).cumsum())# find last grouplabel = max(list(g.groups.keys()))result = df.loc[g.groups[label]._data]# result still includes some rows where the condition is not metresult = result[result > 5]
添加回答
举报
0/150
提交
取消