Zyko
Zyko

Reputation: 65

Skip first n items of GroupBy object

I've grouped my dataframe by the content's IDs with df_grouped = df_input.groupby("ID")

Now, I would like to skip/delete the first n groups ("rows") of the GroupBy object (df_grouped), so that the "new" GroupBy object starts at n+1. Here is some illustration:

| ID | Content |
| ---| --------|
| 3  | just    |
| 4  | a       |
| 6  | short   |
| 10 | sample  |

Skip the first 2 (n=2) groups of df_gouped with something like df_grouped[2:]

| ID | Content |
| ---| --------|
| 6  | short   |
| 10 | sample  |

After skipping the first n groups, the data should still be stored as a GroupBy object.

Upvotes: 0

Views: 134

Answers (1)

mcsoini
mcsoini

Reputation: 6642

Filter first, then group

df.loc[df.ID.isin(df.ID.unique().tolist()[2:])].groupby("ID")

Upvotes: 1

Related Questions