简体   繁体   English

select 第一次出现,其中每个 A(key) 的列值大于 x | dataframe

[英]select first occurrence where column value is greater than x for each A(key) | dataframe

What I have我有的

   A    B    C  D  E
0  foo  0  1.2  1  2
1  foo  1  1.3  2  4
2  foo  2  2.1  4  5
3  foo  3  3.1  3  5
4  nan  0    0  0  0
5  bar  0  4.1  4  6
6  bar  1  1.2  5  8
7  bar  2  1.4  6  9
8  bar  3  5.0  7  9
9  nan  0    0  0  0
10 baz  0  4.1  5  0
11 baz  1  1.2  5  3
12 baz  2  1.4  6  9
13 baz  3  5.0  7  9

What I want, select first occurrence where D >= 4 for each A(key)我想要的是 select 第一次出现,其中每个 A(键)的 D >= 4

So end result will look like,所以最终结果看起来像,

   A    B    C  D  E
0  foo  2  2.1  4  5
1  bar  0  4.1  4  6
2  baz  0  4.1  5  0

You can first slice the rows that match the condition on D, then groupby A and get the first element of each group:您可以先对 D 上符合条件的行进行切片,然后groupby A 分组并获取每个组的first元素:

df[df['D'].ge(4)].groupby('A', sort=False).first()

output: output:

     B    C  D  E
A                
foo  2  2.1  4  5
bar  0  4.1  4  6
baz  0  4.1  5  0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM