Reputation: 191
I have a pandas GroupBy
object. I am using head(k)
to extract the first k elements of each group into a dataframe and I want to also extract the complement. Each group has a nonconstant size.
Is there any straightforwards way of doing this?
Upvotes: 5
Views: 762
Reputation: 153460
You could try this:
Given:
df = pd.DataFrame({'ID':['a','b','b','c','c','c','d','d','d','d'],
'Data':np.arange(10)})
Data ID
0 0 a
1 1 b
2 2 b
3 3 c
4 4 c
5 5 c
6 6 d
7 7 d
8 8 d
9 9 d
df[~df.index.isin(df.groupby('ID').head(2).index)]
Output:
Data ID
5 5 c
8 8 d
9 9 d
Where df.groupby('ID').head(2)
returns:
Data ID
0 0 a
1 1 b
2 2 b
3 3 c
4 4 c
6 6 d
7 7 d
Upvotes: 6
Reputation: 452
Yesy, You can use reindex the new dataframe using the reset_index() method.
Upvotes: 0