2

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?

Chanda Korat
  • 2,453
  • 2
  • 19
  • 23
Mansumen
  • 373
  • 2
  • 4
  • 17
  • what it is your desired output? For instance for uid == 3 ? – edyvedy13 Apr 17 '17 at 09:43
  • @edyvedy13 Since uid==3 has only 2 rows, 2 * 0.2 = 0.4, my desired output for uid==3 is none. For uid==1, I want the following uid iid val 1 5 5.5 – Mansumen Apr 17 '17 at 09:45

2 Answers2

2

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])))
gereleth
  • 2,452
  • 12
  • 21
1

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
piRSquared
  • 285,575
  • 57
  • 475
  • 624