[英]How to filter dataset by number of rows of specific group?
I have a dataset:我有一个数据集:
id value
a1 14
a1 2
a1 34
a1 11
a1 78
b1 11
b1 9
b1 6
I want to filter that dataset by number if rows for each group, to make it no higher than 4. So desired output will be:我想按每个组的行数过滤该数据集,以使其不高于 4。所以所需的 output 将是:
id value
a1 14
a1 2
a1 34
a1 11
b1 11
b1 9
b1 6
How to do that?怎么做?
You can use groupby.head
:您可以使用
groupby.head
:
out = df.groupby('id').head(4)
If you have pandas >=1.4.0, then you can use groupby.nth
with slicing as well:如果您有 pandas >=1.4.0,那么您也可以将
groupby.nth
与切片一起使用:
out = df.groupby('id').nth[:4]
Output Output
id value
0 a1 14
1 a1 2
2 a1 34
3 a1 11
5 b1 11
6 b1 9
7 b1 6
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.