It's straightforward to keep the last N rows for every group in a dataframe with something like df.groupby('ID').tail(N).
In my case, groups have different sizes and I would like to keep the same % of each group rather than same number of rows.
e.g if we want to keep the last 50% rows for each group (based on ID) for the following :
df = pd.DataFrame({'ID' : ['A','A','B','B','B','B','B','B'],
'value' : [1,2,10,11,12,13,14,15]})
The result would be :
pd.DataFrame({'ID' : ['A','A','B','B','B','B','B','B'],
'value' : [2,13,14,15]})
How can we get to that ?
EDIT : If x% is not an int, we round to the smallest closer int.