简体   繁体   English

如何按特定组的行数过滤数据集?

[英]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.

相关问题 通过熊猫检索组中的特定行数 - Retrieving specific number of rows in group by pandas 如何在不进行双重重采样的情况下在重采样之前预过滤到每组的最小行数? - How to pre-filter to a minimum number of rows per group before resampling without double-resampling? 如何按组过滤掉 Pandas DataFrame 行 - How to filter out Pandas DataFrame rows by group 如何根据每个组具有 n 行数的特定列在 pandas 中分组? 如果可能,还要从原始 dataframe 中删除? - How to group by in pandas based on specific columns where each group has n number of rows? Also delete from the original dataframe IF POSSIBLE? Django:如何过滤属于特定组的用户 - Django: How to filter Users that belong to a specific group 如何使用过滤器数据集动态更新 pandas 数据框中的行? - how to use a filter dataset to update rows dynamically in a pandas data frame? 如果行的值是特定数字,则更改 pandas 组中的下一行值 - Change the next rows values in the pandas group if value of a row is a specific number 在 pandas 中保留特定组的一定数量的行 - keep a certain number of rows from a specific group in pandas 如何从数据框中删除一组特定行? - How to remove a group of specific rows from a dataframe? 如何评估一组行是否包含特定值? - How to evaluate if a group of rows contains a specific value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM