Pandas get rows after groupby

我们两清 提交于 2019-12-12 17:03:30

问题


Suppose I have the following dataset:

uid iid val
 1   1   2
 1   2   3
 1   3   4
 1   4  4.5
 1   5  5.5
 2   1   3
 2   2   3
 2   3   4
 3   4  4.5
 3   5  5.5

From this data, I want to first groupby uid, then get last 20% of number of rows from each uid.

That is, since uid=1 has 5 rows, I want to obtain last 1 row (20% of 5) from uid=1.

The following is what I want to do:

df.groupby('uid').tail([20% of each uid])

Can anyone help me?


回答1:


You can try applying a custom function to groupby object. Inside the function calculate how many rows should be taken and take the group's tail with that number of rows. int rounds toward 0, so any groups with less than 5 rows will not contribute any rows to the result.

df.groupby('uid').apply(lambda x: x.tail(int(0.2*x.shape[0])))



回答2:


I'd use floor division

df.groupby('uid').apply(lambda x: x.tail(len(x) // 5))

       uid  iid  val
uid                 
1   4    1    5  5.5

You can avoid including the uid in the index in the first place by passing group_keys=False to the groupby

df.groupby('uid', group_keys=False).apply(lambda x: x.tail(len(x) // 5))

   uid  iid  val
4    1    5  5.5


来源:https://stackoverflow.com/questions/43448895/pandas-get-rows-after-groupby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!